使用json模块实现变量写入文件及读取。
import json
a = {'a':[12,53,96,78,11],'b':[5,5,5,5,5],'c':'测试'}
filename = 'a.json'
with open(filename, 'w',encoding='utf-8') as f:
json.dump(a, f)
with open(filename, 'r',encoding='utf-8') as f:
b = json.load(f)
print(b)
print(type(b))
输出结果:
{‘a’: [12, 53, 96, 78, 11], ‘b’: [5, 5, 5, 5, 5], ‘c’: ‘测试’}