在我的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文件系统, 你不需要把字典转换成字符串,让json帮你把它转换成字符串。你知道吗
同时读取Writing a dict to txt file and reading it back?
干杯!你知道吗
相关问题 更多 >
编程相关推荐