Bil*_*gen 6 python dictionary configparser
我正在尝试将字典的内容写入(并稍后读入)到ConfigParser,我相信我正在根据文档正确地进行,但似乎无法使其工作.有人可以帮忙吗?
import ConfigParser
parser = ConfigParser.ConfigParser()
parser['User_Info'] = {"User1-votes":"36","User1-gamestart":"13232323","User2-votes":"36","User2-gamestart":"234234234","User3-votes":"36","User3-gamestart":"13232323"}
Traceback (most recent call last): File "<stdin>", line 1, in
<module> AttributeError: ConfigParser instance has no attribute '__setitem__'
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是有一个我可以更新的字典,并在最后写入配置文件,所以它看起来像:
[User_Info]
User1-gamestart = 13232323
User3-votes = 36
User2-votes = 36
User1-votes = 36
User2-gamestart = 234234234
User3-gamestart = 13232323
Run Code Online (Sandbox Code Playgroud)
您正在阅读python 3.4的文档,但您可能正在使用旧版本的python.
以下是如何在旧版本的python中使用ConfigParser:
import ConfigParser
parser = ConfigParser.ConfigParser()
info = {"User1-votes":"36","User1-gamestart":"13232323","User2-votes":"36","User2-gamestart":"234234234","User3-votes":"36","User3-gamestart":"13232323"}
parser.add_section('User-Info')
for key in info.keys():
parser.set('User-Info', key, info[key])
with open('config.ini', 'w') as f:
parser.write(f)
Run Code Online (Sandbox Code Playgroud)