两个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)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 09:46:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 09:46:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 09:46:07       82 阅读
  4. Python语言-面向对象

    2024-06-06 09:46:07       91 阅读

热门阅读

  1. 数据结构与算法-15_ B 树

    2024-06-06 09:46:07       22 阅读
  2. 在 Python 中创建一个带有重复键的字典

    2024-06-06 09:46:07       24 阅读
  3. 【组合数学 隔板法 容斥原理】放球问题

    2024-06-06 09:46:07       35 阅读
  4. 常用接口测试及接口抓包常用的测试工具

    2024-06-06 09:46:07       24 阅读
  5. MyBatis使用MySQL5和MySQL8的用法

    2024-06-06 09:46:07       29 阅读
  6. Ubuntu 安装 Vulkan SDK

    2024-06-06 09:46:07       31 阅读
  7. 实战:部署三台kafka服务集群

    2024-06-06 09:46:07       31 阅读