2

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?

skyline_z
  • 97
  • 1
  • 8

2 Answers2

1

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!

0

Use:

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

Avión
  • 7,963
  • 11
  • 64
  • 105
  • Unfortunately, I can not modify this item in the config file because it is used by other module. – skyline_z Oct 22 '15 at 12:27
  • Edit your message and tell us how do you read the file. Put the entire code and specift what can do and what can't you do. – Avión Oct 22 '15 at 12:36