在我保存到.json文件之前,字典中的字符串将等于普通字符串,但在我保存然后从该文件加载之后,字典中的值将不会被视为相等。我已经测试了整数值,那些确实有效,但字符串不适合我。如何让它们彼此相等?
代码:
import json
details = {'apple': 2, 'orange': 3, 'cat': 'tree'}
print("details['cat'] is 'tree':", details['cat'] is 'tree')
with open("example.json", "w") as outfile:
json.dump(details, outfile)
outfile.close()
with open("example.json") as json_file:
loaded_details = json.load(json_file)
json_file.close()
print("loaded_details['cat'] is 'tree':", loaded_details['cat'] is 'tree')
输出:
details['cat'] is 'tree': True
loaded_details['cat'] is 'tree': False