如何在 python configparser 中的值中添加分号? https://stackoverflow.com/questions/4298073/how-do-i-put-a-semicolon-in-a-value-in-python-configparser
Python-2.7
我有一个 python 配置解析器,其中有一个部分,其中键是 url,值是令牌。 url 的键包含:, -, ?和各种其他字符同样适用于值。从上面的问题可以看出,值部分中的特殊字符似乎没问题,但键似乎不太好。
我能做些什么吗?我的替代方案是解析为 json 文件并手动写入/读取它。
例如,如果您在我得到后运行以下程序
cp = ConfigParser.ConfigParser()
cp.add_section("section")
cp.set("section", "http://myhost.com:9090", "user:id:token")
cp.set("section", "key2", "value2")
with open(os.path.expanduser("~/test.ini"), "w") as f:
cp.write(f)
cp = ConfigParser.ConfigParser()
cp.read(os.path.expanduser("~/test.ini"))
print cp.get("section", "key2")
print cp.get("section", "http://myhost.com:9090")
该文件如下所示
[section]
http://myhost.com:9090 = user:id:token
key2 = value2
我遇到了例外ConfigParser.NoOptionError: No option 'http://myhost.com:9090' in section: 'section'