【发布时间】:2015-08-30 20:34:30
【问题描述】:
我正在尝试从文件中读取字典,然后将字符串转换为字典。我有这个,
with open("../resources/enemyStats.txt", "r") as stats:
for line in stats:
if self.kind in line:
line = line.replace(self.kind + " ", "")
line = dict(line)
return line
txt 文件中的行是,
slime {'hp':5,'speed':1}
我希望能够返回一个字典,以便我可以轻松访问敌方角色的 hp 和其他值。
【问题讨论】:
-
你最好使用 json 或 pickle 来存储你的字典
标签: python file dictionary