简体   繁体   中英

How to get nested value from option that are in config.ini file using python

I'm newbie to python and was wondering how to iterate over the following option key1 within section1 below and print comma separated values.

here is the ini file

[section1]
key1 = value1, value2, value3 
key2 = value4, value5, value6

expected output,

value1
value2

I'm using ConfigParser (python 2.6.6), but had no luck so far!

>>> config.get('section1', 'key1')
'value1, value2, value3'

use split to get separated values:

>>> key1 = config.get('section1', 'key1').split(', ')
>>> key1
['value1', 'value2', 'value3']

>>> for v in key1:
...  print v
... 
value1
value2
value3

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