[英]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: 我用configobj读取配置文件为:
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 - 我遇到了类似的问题,并通过子类化ConfigObj并覆盖删除和添加引号的方法来“修复”它-
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. 然后,使用MyConfigObj代替ConfigObj,并读入并写回带有引号的配置条目,而无需进行更改。
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! 这适用于我使用过的简单配置文件,但是我想在更复杂的配置文件(多行条目,带有列表的条目等)上,还需要进一步完善!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.