json介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
代码部分:
import json
a_dict = {'user_id':'qbf','user_name':'hello',100:200} #定义一个字典
#把字典信息保存在本地
with open('example.json','w') as f:
json.dump(a_dict,f)#其他数据类型也可以
#从文件中获取数据
with open('example.json') as f:
content = json.load(f)
print(content)
结果:
{'user_name': 'hello', 'user_id': 'qbf', '100': 200}
网友评论