标签:open 包含 import 就是 python 类型 数据类型 int print
import json lt = [ {‘name‘: ‘王宝强‘, ‘age‘: 30}, {‘name‘: ‘贾乃亮‘, ‘age‘: 36}, {‘name‘: ‘马蓉蓉‘, ‘age‘: 33}, {‘name‘: ‘宋吉吉‘, ‘age‘: 40}, {‘name‘: ‘李小璐‘, ‘age‘: 43}, ] # json字符串<------dumps------python数据类型 # ------loads-------> # 包含json的类文件对象<------dump------python数据类型 # ------load-------> # 具有read()或者write()方法的对象就是类文件对象 # f = open(“a.txt”,”r”) f就是类文件对象 string = json.dumps(lt) print(type(string)) # <class ‘str‘> # json字符串 print(string) # [{"name": "\u738b\u5b9d\u5f3a", "age": 30}, {"name": "\u8d3e\u4e43\u4eae", "age": 36}, {"name": "\u9a6c\u84c9\u84c9", "age": 33}, {"name": "\u5b8b\u5409\u5409", "age": 40}, {"name": "\u674e\u5c0f\u7490", "age": 43}] obj = json.loads(string) print(type(obj)) # <class ‘list‘> json.dump(lt, open(‘json.txt‘, ‘w‘, encoding=‘utf8‘)) obj = json.load(open(‘json.txt‘, ‘r‘, encoding=‘utf8‘)) print(type(obj)) # <class ‘list‘> print(obj) # [{‘name‘: ‘王宝强‘, ‘age‘: 30}, {‘name‘: ‘贾乃亮‘, ‘age‘: 36}, {‘name‘: ‘马蓉蓉‘, ‘age‘: 33}, {‘name‘: ‘宋吉吉‘, ‘age‘: 40}, {‘name‘: ‘李小璐‘, ‘age‘: 43}]
标签:open 包含 import 就是 python 类型 数据类型 int print
原文地址:https://www.cnblogs.com/yzg-14/p/12121863.html