16
我想写我的第一个JSON文件。但出于某种原因,它不会真正写入文件。我知道它正在做一些事情,因为在运行转储之后,我放入文件中的任何随机文本都将被删除,但没有任何替代文本。不用说,但负载部分抛出和错误,因为没有什么。不应该将所有的json文本添加到文件中吗?蟒蛇,写Json到文件
from json import dumps, load
n = [1, 2, 3]
s = ["a", "b" , "c"]
x = 0
y = 0
with open("text", "r") as file:
print(file.readlines())
with open("text", "w") as file:
dumps({'numbers':n, 'strings':s, 'x':x, 'y':y}, file, indent=4)
file.close()
with open("text") as file:
result = load(file)
file.close()
print (type(result))
print (result.keys())
print (result)