两个jsonl文件a和b,如何读取a文件中desc对应的值和type对应的值,然后在b文件中找到desc对应值相同的数据并把type的值写入?

两个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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值