问题:
我在文件夹json_data = open("C:/Users/Desktop/soccer_data2/*.json")
a-01.json
a-02.json
a-03.json
a-01.json :
{'blabla': '127',
'blabla': 'Sun,,26,Oct,2014',
'events': [{'outcome': 'save',
'playerId': 124,
'position': ['0,50'],
'teamId': 16,
'timestamp': 294,
'type': 'goal_keeping'},
{'outcome': 'save',
'playerId': 434,
'position': ['0,50'],
'teamId': 19,
'timestamp': 744,
'type': 'goal_keeping'},
a-02.json :
{'away_team': '112',
'date': 'Sun,,27,Oct,2014',
'events': [{'outcome': 'save',
.
我想在一个json中合并所有文件。 有可能的? 感谢所有
答案 0 :(得分:0)
这是只是一个模板我在这里写的没有测试它,所以它可能有一些错误或错字,如果有人有评论我会很感激。
import os # for manipulates files and subdirectories
import json # handle json files
json_folder_path = os.path.join("C","Users","Desktop","soccer_data2")
# In order to get the list of all files that ends with ".json"
# we will get list of all files, and take only the ones that ends with "json"
json_files = [ x for x in os.listdir(json_folder_path) if x.endswith("json") ]
json_data = list()
for json_file in json_files:
json_file_path = os.path.join(json_folder_path, json_file)
with open (json_file_path, "r") as f:
json_data.append(json.load(f))
# now after iterate all the files, we have the data in the array, so we can just write it to file
output_path = os.path.join(json_folder_path,"output.json")
with open (output_path, "w") as f:
json.dump(json_data, f)