【问题标题】:How to open multiple JSON file from folder and merge them in single JSON file in python?如何从文件夹中打开多个 JSON 文件并将它们合并到 python 中的单个 JSON 文件中?
【发布时间】:2019-11-17 09:18:23
【问题描述】:

假设有 3 个文件 - data1.json、data2.json、data3.json。

假设 data1.json 包含 -

{ 
   "Players":[ 
      { 
         "name":"Alexis Sanchez",
         "club":"Manchester United"
      },
      { 
         "name":"Robin van Persie",
         "club":"Feyenoord"
      }
   ]
}

data2.json 包含 -

{ 
   "Players":[ 
      { 
         "name":"Nicolas Pepe",
         "club":"Arsenal"
      }
   ]
}

data3.json 包含 -

{ 
   "players":[ 
      { 
         "name":"Gonzalo Higuain",
         "club":"Napoli"
      },
      { 
         "name":"Sunil Chettri",
         "club":"Bengaluru FC"
      }
   ]
}

这 3 个文件的合并将生成一个包含以下数据的文件。 结果.json -

{ 
   "players":[ 
      { 
         "name":"Alexis Sanchez",
         "club":"Manchester United"
      },
      { 
         "name":"Robin van Persie",
         "club":"Feyenoord"
      },
      { 
         "name":"Nicolas Pepe",
         "club":"Arsenal"
      },
      { 
         "name":"Gonzalo Higuain",
         "club":"Napoli"
      },
      { 
         "name":"Sunil Chettri",
         "club":"Bengaluru FC"
      }
   ]
}

如何从文件夹中打开多个 JSON 文件并在 python 中将它们合并到单个 JSON 文件中?

我的方法:

import os, json
import pandas as pd
path_to_json =  #path for all the files.
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

jsons_data = pd.DataFrame(columns=['name', 'club'])

for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)

        name = json_text['strikers'][0]['name']
        club = json_text['strikers'][0]['club']

        jsons_data.loc[index] = [name, club]

print(jsons_data)

【问题讨论】:

  • 你尝试过什么,到底有什么问题?
  • 上述编辑 --> 我的方法是我尝试过的。
  • 请看一下格式,这是不可读的。无论如何,我想知道你为什么要使用熊猫,这只会给你带来额外的复杂性恕我直言。也就是说,读取数据、处理数据、写入数据,这些都是您需要执行的步骤,您也应该尝试分别解决这些问题。

标签: python json


【解决方案1】:

这可能适合你:

import json
import glob
import pprint as pp #Pretty printer

combined = []
for json_file in glob.glob("*.json"): #Assuming that your json files and .py file in the same directory
    with open(json_file, "rb") as infile:
        combined.append(json.load(infile))



pp.pprint(combined)

【讨论】:

【解决方案2】:

这正是你想要的,

import json, glob

merged_json = []
for json_file in glob.glob("*json"):
    with open(json_file, "rb") as file:
      json_data = json.load(file)
      if "Players" in json_data:
        merged_json += json_data["Players"]
      else:
        merged_json += json_data["players"]

to_json = json.dumps(merged_json)
print (to_json)

输出

[{"name": "Alexis Sanchez", "club": "Manchester United"}, {"name": "Robin van Persie", "club": "Feyenoord"}, {"name": "Nicolas Pepe", "club": "Arsenal"}, {"name": "Gonzalo Higuain", "club": "Napoli"}, {"name": "Sunil Chettri", "club": "Bengaluru FC"}]

【讨论】:

  • 此答案是@Jim Erginbash 答案的略微修改版本。他读取多个 JSON 文件的方法比我最初使用的方法要好。
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 2022-01-23
  • 2019-12-16
  • 1970-01-01
  • 2020-11-10
  • 2019-01-20
  • 2020-09-05
  • 1970-01-01
相关资源
最近更新 更多