You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python拆分JSON文件失败,求修正:拆分头部与in_network字段

问题需求

需要将JSON文件拆分为两部分:

  • 头部:包含"in_network"键之前的所有信息
  • 详情:包含"in_network"键对应的全部内容

目标JSON文件

{
  "reporting_entity_name": "launcher",
  "reporting_entity_type": "launcher",
  "plan_name": "launched",
  "plan_id_type": "hios",
  "plan_id": "1111111111",
  "plan_market_type": "individual",
  "last_updated_on": "2020-08-27",
  "version": "1.0.0",
  "in_network": [
    {
      "negotiation_arrangement": "ffs",
      "name": "Boosters",
      "billing_code_type": "CPT",
      "billing_code_type_version": "2020",
      "billing_code": "27447",
      "description": "Boosters On Demand",
      "negotiated_rates": [
        {
          "provider_groups": [
            {
              "npi": [
                0
              ],
              "tin": {
                "type": "ein",
                "value": "11-1111111"
              }
            }
          ],
          "negotiated_prices": [
            {
              "negotiated_type": "negotiated",
              "negotiated_rate": 123.45,
              "expiration_date": "2022-01-01",
              "billing_class": "organizational"
            }
          ]
        }
      ]
    }
  ]
}

原执行代码

flag = 0
file = open('new_test.JSON', 'r')
detail_file_1 = open('new_test_detail.json', 'a')
detail_file_1.write('{')
detail_file_1.close()
for line in file:
    if flag == 0:
        if line != '  "in_network": ['
            header_file = open('new_test_header.json', 'a')
            header_file.write(line)
            header_file.close()
        else:
            flag = 1
    else:
        detail_file = open('new_test_detail.json', 'a')
        detail_file.write(line)
        detail_file.close()
header_file_1 = open('new_test_header.json', 'a')
header_file_1.write('}')
header_file_1.close()

预期输出

头部文件(new_test_header.json)

{
  "reporting_entity_name": "launcher",
  "reporting_entity_type": "launcher",
  "plan_name": "launched",
  "plan_id_type": "hios",
  "plan_id": "1111111111",
  "plan_market_type": "individual",
  "last_updated_on": "2020-08-27",
  "version": "1.0.0",
}

详情文件(new_test_detail.json)

{
"in_network": [
    {
      "negotiation_arrangement": "ffs",
      "name": "Boosters",
      "billing_code_type": "CPT",
      "billing_code_type_version": "2020",
      "billing_code": "27447",
      "description": "Boosters On Demand",
      "negotiated_rates": [
        {
          "provider_groups": [
            {
              "npi": [
                0
              ],
              "tin": {
                "type": "ein",
                "value": "11-1111111"
              }
            }
          ],
          "negotiated_prices": [
            {
              "negotiated_type": "negotiated",
              "negotiated_rate": 123.45,
              "expiration_date": "2022-01-01",
              "billing_class": "organizational"
            }
          ]
        }
      ]
    }
  ]
}

解决方案

原代码存在以下问题:

  1. 字符串匹配未考虑换行符:读取的每行末尾包含\n,直接和' "in_network": ['匹配永远不成立
  2. 频繁打开/关闭文件:循环内每次操作都打开关闭文件,效率低且易出错
  3. 未将"in_network"行写入详情文件:切换flag时跳过了该行
  4. 语法错误:if line != ' "in_network": ['缺少闭合括号

修改后的代码如下:

# 使用with语句管理文件,自动处理关闭,避免资源泄漏
with open('new_test.JSON', 'r') as src_file, \
     open('new_test_header.json', 'w') as header_file, \
     open('new_test_detail.json', 'w') as detail_file:
    
    flag = 0
    detail_file.write('{\n')  # 写入详情文件的开头括号
    
    for line in src_file:
        stripped_line = line.strip()
        # 用包含判断匹配目标行,忽略前后空格和换行,更鲁棒
        if '"in_network":' in stripped_line:
            flag = 1
            detail_file.write(line)  # 将目标行写入详情文件
        elif flag == 0:
            header_file.write(line)
        else:
            detail_file.write(line)
    
    # 给头部文件添加闭合括号,保持格式一致
    header_file.write('  }\n')

代码说明

  • 用with语句统一管理文件生命周期,避免手动关闭的繁琐和资源泄漏
  • 改用包含判断匹配目标行,忽略换行和空格干扰,适配不同格式的JSON行
  • 切换flag时直接将"in_network"行写入详情文件,避免遗漏
  • 使用w模式覆盖写入,避免文件原有内容干扰拆分结果

内容的提问来源于stack exchange,提问作者Anjaney Shrivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 23:25:19