Python configparser reading section and creating new config

135 Views Asked by At

I'm currently reading a config file getting all values from a specific section and printing them however, instead of printing them I would like to create a new config file with just that section I am reading.

How would I go about this?

code

configFilePath = 'C:\\testing.ini'
config = ConfigParser.ConfigParser()
    config.optionxform = str
    config.read(configFilePath)
    section = 'testing1'
    configdata = {k:v for k,v in config.items(section)}
    for x in configdata.items(): 
        print x[0] + '=' + x[1]

config ini

[testing1]
Español=spain
UK=unitedkingdom
something=somethingelse

[dontneed]
dontneedthis=blahblah
dontneedthis1=blahblah1

Also while I'm here, I'm not sure how I would get this to work with encoded strings like "ñ" as it errors, however I need my new config file exactly how I'm reading it.

1

There are 1 best solutions below

0
Ranga Sarin On

I got it working with

for x in configdata.items(): 
    confignew.set(section,x[0],x[1]) 
confignew.write( EqualsSpaceRemover( cfgfile ) ) 

however, how would I edit my code so it can read text with characters like " ñ " and parse/write them without getting errors about decode problems?