簡體   English   中英

在python中合並json文件時出現問題

[英]issue in merging json files in python

我試圖合並具有相同標題的多個json文件。 我使用了其他文章中討論的代碼,並對目標文件夾進行了如下調整:

  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)

當我循環合並時,它顯示了一些錯誤,如下所示:

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

您可以在此處下載示例文件。

就我而言,我有100個以上的json文件要合並為一個。 誰能建議它如何合並?

問候Zep。

問題在於您對glob的使用情況。 這是一個用法示例。

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

現在,您可以像以前一樣對all_json_files使用for循環

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM