简体   繁体   中英

ConfigParser with no delimiter

Using Python 3.5 and ConfigParser.

I want to use a config file like this:

[Section]
key1
key2
key3

ie no values. By default ConfigParser requires values but I can pass allow_no_values=True to the constructor to handle that.

However the parser will still try to split on the delimiters which by default are ('=', ':') . Thus my lines can't include any of them by default. But I don't want to delimit on anything - none of my lines will ever have a value.

Passing delimiters=() or [] or None does not work. If using an empty list it complains that option '' in section 'Section' already exists while if None I get 'NoneType' is not iterable .

So is there no way to make sure that splitting never happens ? It does not feel optimal that I have to specify some char that I "hope" will never be used.

你可以设置delimiters=('\\n',) ,理论上它意味着键/值分隔符是一个换行符,它永远不会发生,因为行分隔符也是一个换行符,它似乎优先。

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