我有这个配置文件
[section1]
namespace = {'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}
使用ConfigParser读取文件我得到了:
>>> from ConfigParser import ConfigParser
>>> Config = ConfigParser()
>>> Config.read("myfile.ini")
>>> name = Config.get("section1", "namespace")
>>> name
"{'pro1':'http://pro1/go','pro2':'http://pro2/go','pro3':'http://pro3/go'}"
将字符串转换为dict / json:
>>> import json
>>> namespace = json.loads(name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
>>>
有什么想法吗?