读取 json 文件,查找并替换双引号并写入新的 json 文件

Read a json file, find and replace double quotes and write new json file

我有一个 JSON 文件,它看起来像:

[
 {
   "story_id": xx,
   "line_number": 109,
   "sentence": "fhsabdajbndkjlabhfegbdajbdhj",
   "ner": "{'gfjghj': 'PERSON', 'hjbhjb': 'DATE'}",
   "PROPN": "['vhjb', 'ghjhb']",
   "Best": 1
 }
]

我想在标签 ner 和 PROPN 中找到开始和结束 "(双引号)并用空替换。

输出应该是一个 json 文件,数据应该是这样的:

 [
 {
   "story_id": xx,
   "line_number": 109,
   "sentence": "fhsabdajbndkjlabhfegbdajbdhj",
   "ner": {'gfjghj': 'PERSON', 'hjbhjb': 'DATE'},
   "PROPN": ['vhjb', 'ghjhb'],
   "Best": 1
 }
]    

我试过这个:

import json
with open('path/to/file.json','r',encoding ='utf-8') as f:
    data = json.load(f)

for item in data:
    item['ner'] = item['ner'].replace('"{', '{').replace('}"', '}').replace('"[', '[').replace(']"', ']')

with open('path/to/output_file.json'', 'w') as f:
    json.dump(data, f)

虽然 运行 这个,我得到 "UnicodeDecodeError"。

有人可以帮忙吗?

提前致谢。

使用ast模块

例如:

import json
with open('path/to/file.json','r',encoding ='utf-8') as f:
    data = json.load(f)

for item in data:
    item['ner'] = ast.literal_eval(item['ner'])

with open('path/to/output_file.json'', 'w') as f:
    json.dump(data, f)