我想把一些配置值保存到一个文本文件中,以便以后在代码中使用它们,所以我决定以JSON格式将其保存到一个文本文件中,但是当我试图从文件中读取值时,我遇到了一个错误
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
文本文件内容为:
"{'EditDate': 1497014759002}"
import json
import os
cPath = os.path.dirname(os.path.realpath(__file__))
configPath = cPath+'/tt.txt'
ConfigStr = {"EditDate" : 1497014759002}
print(ConfigStr)
print("-----------")
with open(configPath, 'w') as outfile:
json.dump(repr(ConfigStr), outfile)
with open(configPath) as json_data:
d = json.load(json_data)
jstr = d
print(jstr)
print("-----------")
a = json.loads(jstr)
lastedit = a['EditDate']
print(lastedit)
您应该使用
json.dump
将其转储到文件中。将要写入的对象和类似文件的对象传递给它。在参考:
https://docs.python.org/2/library/json.html#json.dump
相关问题 更多 >
编程相关推荐