两个jsonl文件a和b,如何读取a文件中desc对应的值和type对应的值,然后在b文件中找到desc对应值相同的数据并把type的值写入?
import json
# 读取a.jsonl文件中的数据
def read_jsonl(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return [json.loads(line.strip()) for line in f]
# 写入jsonl文件
def write_jsonl(file_path, data):
with open(file_path, 'w', encoding='utf-8') as f:
for item in data:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
# 从a.jsonl读取desc和type
a_data = read_jsonl('tvr_val_release.jsonl')
desc_type_map = {item['desc']: item['type'] for item in a_data if 'desc' in item and 'type' in item}
# 从b.jsonl读取数据并更新type
b_data = read_jsonl('tvr_cap_composed_val.jsonl')
for item in b_data:
if 'desc' in item and item['desc'] in desc_type_map:
item['type'] = desc_type_map[item['desc']]
# 将更新后的数据写回b.jsonl
write_jsonl('tvr_cap_composed_val.jsonl', b_data)