【发布时间】:2019-09-11 01:15:11
【问题描述】:
在我的 python 程序中,我有一个添加到文本文件中的键和值列表,我需要能够提取此字符串列表并将其转换为字典。
例如:
counter = 0
add = str(counter)
counterDict = UserID + ": " + add + ", "
counter = counter + 1
#This should make counterDict look like: ""Smi39119": 0, "Joh38719": 1, " etc.
f = open("Dictionaries.txt","a")
f.write(counterDict)
f.close()
#Then I would like to be able to open the file and turn that string into a dictionary
f = open("Dictionaries.txt","r")
string = f.read()
#This way 'string' should still be a string, but look like this: "{"Smi39119": 0, "Joh38719": 1, }"
我不知道这是否可行,但如果可以,所有解决方案将不胜感激。
【问题讨论】:
-
json模块? -
docs.python.org/3/library/json.html 正是您所需要的。
标签: python python-3.x dictionary text