1. 读取:
然后通过 key 值访问:
with open('1.json','r',encoding='utf8') as f:
json_data = json.load(f)
for json_dict in json_data:
_id = json_dict['姓名']
print("The id is {}".format(_id))
>>
[
{"姓名": "张三","年龄": 25},
{"姓名": "李四","年龄": 26}
]
- 如果有多行文件,上述读取会出错,修改为:
这种针对的是 一行是一个 json{} 的数据情况:
{"text": "如何演好自己的角色,请读《演员自我修养》《喜剧之王》周星驰崛起于穷困潦倒之中的独门秘笈", "spo_list": [{"predicate": "主演", "object_type": "人物", "subject_type": "影视作品", "object": "周星驰", "subject": "喜剧之王"}]}
{"text": "茶树茶网蝽,Stephanitis chinensis Drake,属半翅目网蝽科冠网椿属的一种昆虫", "spo_list": [{"predicate": "目", "object_type": "目", "subject_type": "生物", "object": "半翅目", "subject": "茶树茶网蝽"}]}
import json
with open(r"D:\project\zhangyi\relation_extract\美亚数据\all_data.json", 'r',encoding='utf-8') as file:
for line in file.readlines():
js_l = json.loads(line)
print(js_l)
1. json.dumps() 序列,中文出现 \u 编码
text = json.dumps(InputStr,ensure_ascii=False)
网友评论