''' python中提供一个特殊的模块,可以直接对python 中的数据进行序列化操作 序列化:按照指定的数据顺序定义数据格式【类似编码】 '''
# 进入json
import json
# 准备操作的数据
users = {"admin": {"username": "admin", "password": "123", "nickname": "老刘"}}
1.将程序中的数据,直接存储到文件中
# json模块的操作
with open("./data/3.1.json", "w") as file:
json.dump(users,file)
# dump:添加
2. 将文件中的数据读取到程序中
with open("./data/3.1.json", "r") as file:
# 查看此时的文件及类型
print(file,type(file))
<_io.TextIOWrapper name='./data/3.1.json' mode='r' encoding='cp936'> <class '_io.TextIOWrapper'>
users = json.load(file)
print(users,type(users))
{'admin': {'username': 'admin', 'password': '123', 'nickname': '老刘'}} <class 'dict'>