【发布时间】:2013-07-30 12:41:21
【问题描述】:
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'
【问题讨论】:
标签: python python-2.7