简体   繁体   中英

Parsing multiple lines in config file divided by + sign in Python

I have config file like:

[Default]
Mykey=123,456,789
+234,567,891
+345,678,912

Can you give me suggestions how to parse them using ConfigParser ( python ) so I can have these values in array or list for example:

import configparser
conf = configparser.ConfigParser()
conf.read(r"c:\tmp\myini.ini")

after that I want to have these lines parased like

conf["Default"]["Mykey"] --> returns array/list with parsed values

Result should be like:

print(conf["Default"]["MyKey"])
['123,456,789','234,567,891','345,678,912']

You need to change your ini file format to

[Default]
Mykey:['123,456,789', '+234,567,891', '+345,678,912']

After this you can access it like

print conf["Default"]["Mykey"]

Result :

['123,456,789', '+234,567,891', '+345,678,912']

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