简体   繁体   中英

Python configparser reading section and creating new config

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM