我有一个字符串如下
"{"V":1,"Batch":10001,"File":"abc.csv","ContactorID":"A001"}"
我实际上如何将这个字符串转换成字典?字符串源自txt文件
您应该使用json模块。您可以打开文件并使用,json.load(file)
也可以将文本作为字符串包含在程序中并执行json.loads(text)
。例如:
import json
with open('file.txt', 'r') as file:
dict_from_file = json.load(file)
要么
import json
text = '{"V":1,"Batch":10001,"File":"abc.csv","ContactorID":"A001"}'
dict_from_text = json.loads(text)
有关更多信息,请参见https://realpython.com/python-json/。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句