Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python加载和读取文件夹中的所有json文件_Python_Json_File_Merge_Concatenation - Fatal编程技术网

使用python加载和读取文件夹中的所有json文件

使用python加载和读取文件夹中的所有json文件,python,json,file,merge,concatenation,Python,Json,File,Merge,Concatenation,问题是: 我在一个文件夹中有一系列文件json\u data=open(“C:/Users/Desktop/soccer\u 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,

问题是:

我在一个文件夹中有一系列文件
json\u data=open(“C:/Users/Desktop/soccer\u 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中。 有可能吗?
多亏了所有的这是一个模板,我在这里没有测试它,所以它可能有一些错误或打字错误,如果有人有意见,我将不胜感激

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)

这只是一个模板,我在这里写的,没有测试过,所以可能有一些错误或打字错误,如果有人有意见,我将不胜感激

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)

开始编码,看看会发生什么。所以是关于修正你的代码,而不是实现你的想法。请反复阅读,如果您有问题,请提供您的代码。如果遇到错误,请将错误消息逐字复制并粘贴到问题中。除非需要传达布局错误,否则避免使用屏幕截图。我们无法将您的图像复制并粘贴到我们的IDE中以修复您的代码。请不要忘记研究,f.e.:and here:and here:and here:or:另外,您的问题陈述不明确。你说的“合并”到底是什么意思?是否希望列表中的每个对象都代表一个原始文件?是否需要将文件名映射到其内容的词典?是否要为每个文件调用
dict.update()
后生成一个字典?了解如何使用fs模块(读取/写入文件)和json模块(将对象转换为json或从json转换)。请阅读以下背景:。我的问题是按顺序一个接一个地加载文件(如果可能的话,使用for循环),可能在连接完文件后开始编码,看看会发生什么。所以是关于修正你的代码,而不是实现你的想法。请反复阅读,如果您有问题,请提供您的代码。如果遇到错误,请将错误消息逐字复制并粘贴到问题中。除非需要传达布局错误,否则避免使用屏幕截图。我们无法将您的图像复制并粘贴到我们的IDE中以修复您的代码。请不要忘记研究,f.e.:and here:and here:and here:or:另外,您的问题陈述不明确。你说的“合并”到底是什么意思?是否希望列表中的每个对象都代表一个原始文件?是否需要将文件名映射到其内容的词典?是否要为每个文件调用
dict.update()
后生成一个字典?了解如何使用fs模块(读取/写入文件)和json模块(将对象转换为json或从json转换)。也请阅读以下背景:。我的问题是按顺序一个接一个地加载文件(如果可能的话,使用for循环),可能是在连接完HsOnDecodeError:Unterminated string开始于:第1行第143359列(char 143358)。我相信您现在失败的原因是因为您的json文件有问题。但是复制和粘贴错误对你没有帮助,请在Goggle中搜索。我发现文件已损坏,多亏了你的代码,我解决了所有问题,再次感谢你的支持!JSONDecodeError:Unterminated字符串起始于:第1行第143359列(char 143358)。我认为您现在失败的原因是您的json文件有问题。但是复制和粘贴错误对你没有帮助,请在Goggle中搜索。我发现文件已损坏,多亏了你的代码,我解决了所有问题,再次感谢你的支持!