python存储数据json

python存储数据json
使用json模块来存储数据,模块json让你能够将简单的Python数据结构存储到文件中,并在程序再次运行时加载文件中的数据,也可以使用json在程序之间分析数据,更重要的是,json数据格式并非python专用的,其他编程语言也可以用。

json.dump()       存储数据,接受两个实参:要存储的数据,存储数据的文件对象
json.load()         导入json数据
#用json存储数据
import json
#写入数据
numbers = [1,2,3,4,5]

filename = "numbers.json"
with open(filename,'w') as f_obj:
    json.dump(numbers,f_obj)


#读数据
with open(filename) as f_obj:
    number = json.load(f_obj)

print(number)

猜你喜欢

转载自blog.csdn.net/weixin_43670105/article/details/88104104
今日推荐