【问题标题】:using Python Merge two json file with each file having multiple json objects使用 Python 合并两个 json 文件,每个文件有多个 json 对象
【发布时间】:2021-09-19 16:18:32
【问题描述】:

我对 Python 很陌生,我需要根据 "Id" 将两个 json 文件与多个 json 对象合并。

文件1.json

{"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"}
{"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"}
{"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"}
{"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}

文件2.json

{"id": 4, "math": "A", "class": 8, "physics": "D"}
{"id": 2, "math": "B", "class": 8, "physics": "C"}
{"id": 3, "math": "A", "class": 8, "physics": "A"}
{"id": 1, "math": "C", "class": 8, "physics": "B"}

json.loads(jsonObj)json.load(path) 我都试过了。两者都抛出错误。 我知道这两个文件作为一个整体(组合)都不是有效的 json,但文件中的每一行都是有效的 json。我想逐行阅读并合并两者。

【问题讨论】:

  • list1 = [json.loads(line) for line in open(path)] 然后你可以发布你的合并尝试(提示:这里有很多关于合并字典的问题)

标签: python json python-3.x object merge


【解决方案1】:

你可以逐行阅读然后解析

# asuming

# File1.json

# {"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"}
# {"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"}
# {"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"}
# {"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}

# File2.json

# {"id": 4, "math": "A", "class": 8, "physics": "D"}
# {"id": 2, "math": "B", "class": 8, "physics": "C"}
# {"id": 3, "math": "A", "class": 8, "physics": "A"}
# {"id": 1, "math": "C", "class": 8, "physics": "B"}


import json
merged = {}

with open('File1.json') as f:
    for line in f:
        jsonified = json.loads(line)
        merged[jsonified['id']] = jsonified
        
with open('File2.json') as f:
    for line in f:
        jsonified = json.loads(line)
        merged[jsonified['id']].update(jsonified) # asuming both file has same ids otherwise use try catch

merged = list(merged.values())
print(merged)
[{'id': 1,
  'name': 'Ault',
  'class': 8,
  'email': 'ault@pynative.com',
  'math': 'C',
  'physics': 'B'},
 {'id': 2,
  'name': 'john',
  'class': 8,
  'email': 'jhon@pynative.com',
  'math': 'B',
  'physics': 'C'},
 {'id': 3,
  'name': 'josh',
  'class': 8,
  'email': 'josh@pynative.com',
  'math': 'A',
  'physics': 'A'},
 {'id': 4,
  'name': 'emma',
  'class': 8,
  'email': 'emma@pynative.com',
  'math': 'A',
  'physics': 'D'}]

【讨论】:

    【解决方案2】:

    这是我使用pandas 对此的看法。

    import pandas as pd
    import os
    
    os.chdir(os.getcwd())
    file_path_1 = 'file1.json'
    file_path_2 = 'file2.json'
    
    df1 = pd.read_json(file_path_1, lines=True)
    df2 = pd.read_json(file_path_2, lines=True)
    
    df = df1.merge(df2, on='id')
    
    print(df)
    

    输出:

       id  name  class_x              email math  class_y physics
    0   1  Ault        8  ault@pynative.com    C        8       B
    1   2  john        8  jhon@pynative.com    B        8       C
    2   3  josh        8  josh@pynative.com    A        8       A
    3   4  emma        8  emma@pynative.com    A        8       D
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 2015-05-13
      • 1970-01-01
      相关资源
      最近更新 更多