简体   繁体   English

在python中合并json文件时出现问题

[英]issue in merging json files in python

I was trying to merge multiple json files with the same headers. 我试图合并具有相同标题的多个json文件。 I used the the code discussed in other posts and tweaked a bit on destination folder as follow: 我使用了其他文章中讨论的代码,并对目标文件夹进行了如下调整:

  import glob
  result= []
  filepath = 'D:/Football matters/Sttratagem data access/WYScout/trial data/MergeTest'
  all_header_files = glob.glob(filepath+'/*.json')

    for f in all_header_files:
        with open(f, "rb") as infile:
            result.append(json.load(infile))

    with open("merged_file.json", "wb") as outfile:
         json.dump(result, outfile)

When I loop it to merge, it showed some error as follow: 当我循环合并时,它显示了一些错误,如下所示:

TypeError: a bytes-like object is required, not 'str'

You can download the sample file Here . 您可以在此处下载示例文件。

In my case, I have 100 over json files to merge into one. 就我而言,我有100个以上的json文件要合并为一个。 Can anyone advise it how to merge? 谁能建议它如何合并?

Regards Zep. 问候Zep。

The issue is in your usage of glob. 问题在于您对glob的使用情况。 Here's an example of its usage. 这是一个用法示例。

import glob
filepath = 'D:/Football matters/Sttratagem data access/WYScout/trialdata/MergeTest'
all_json_files = glob.glob(filepath+'/*.json')

You can now use a for loop as before with all_json_files 现在,您可以像以前一样对all_json_files使用for循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM