从json文件加载后如何使相同值的字符串相等

时间:2016-01-11 20:50:55

标签: python json

在我保存到.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

0 个答案:

没有答案