简体   繁体   中英

How to keep the quotes string with configobj in Python

I have a config file and it has an item as:

[time_format]
iso = "ISO (2015-12-31 13:30:55)"

I read the config file with configobj as:

config = ConfigObj(file_name);
section = config.keys();

After some reading, I write the config without modifying the above item as:

config.write();

The item in the config file becomes:

iso = ISO (2015-12-31 13:30:55)

The quotes disappear. Is there any way to keep the quotes?

采用:

iso = '"ISO (2015-12-31 13:30:55)"'

I had a similar problem and 'fixed' it by subclassing ConfigObj and overwriting the methods that remove and add the quotes -

from configobj import ConfigObj

class MyConfigObj(ConfigObj):

    def __init__(self, *args, **kwargs):
        ConfigObj.__init__(self, *args, **kwargs)

    def _unquote(self, value):
         return value

    def _quote(self, value, multiline=True):
        return value

Then use MyConfigObj in place of ConfigObj and config entries with quotation marks are read in and written back without change.

This works on the simple config files I have used, but I imagine on more complex config files (multiline entries, entries with lists etc.) further refinement will be needed!

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