import json
#AttributeError: module ‘json’ has no attribute ‘dump’
#模块的名字被我命名成了json.py,名称冲突
#使用json.dump()和json.load()
numbers = [2, 3, 4, 5, 66, 12]
filename = './data/number.json'
with open(filename,'w') as file:
json.dump(numbers,file)
with open(filename) as f:
numbers = json.load(f)
print(numbers)
#json.dumps() 将JSON对象转换为JSON字符串.
filename = "./data/numbers.json"
with open(filename, "w") as f:
number_string = json.dumps(numbers)
f.write(number_string)
#json.loads() 将JSON字符串转换为JSON对象
with open(filename) as f:
number_string = f.read()
numbers = json.loads(number_string)
print(numbers)
Python编程从入门到实践~JSON
最新推荐文章于 2023-03-24 14:47:38 发布