こんにちは、次jsonlのような 2 つのファイルがあります。
one.jsonl
{"name": "one", "description": "testDescription...", "comment": "1"}
{"name": "two", "description": "testDescription2...", "comment": "2"}
second.jsonl
{"name": "eleven", "description": "testDescription11...", "comment": "11"}
{"name": "twelve", "description": "testDescription12...", "comment": "12"}
{"name": "thirteen", "description": "testDescription13...", "comment": "13"}
そして、私の目標は、次のような新しいjsonlファイル名 (エンコーディングを保持したまま)を作成merged_file.jsonlすることです。
{"name": "one", "description": "testDescription...", "comment": "1"}
{"name": "two", "description": "testDescription2...", "comment": "2"}
{"name": "eleven", "description": "testDescription11...", "comment": "11"}
{"name": "twelve", "description": "testDescription12...", "comment": "12"}
{"name": "thirteen", "description": "testDescription13...", "comment": "13"}
私のアプローチは次のようなものです:
import json
import glob
result = []
for f in glob.glob("folder_with_all_jsonl/*.jsonl"):
with open(f, 'r', encoding='utf-8-sig') as infile:
try:
result.append(extract_json(infile)) #tried json.loads(infile) too
except ValueError:
print(f)
#write the file in BOM TO preserve the emojis and special characters
with open('merged_file.jsonl','w', encoding= 'utf-8-sig') as outfile:
json.dump(result, outfile)
しかし、私はこのエラーに遭遇しました:
TypeError: Object of type generator is not JSON serializable私はあなたのヒント/ヘルプを何らかの方法で感謝します. ありがとうございました!私は他のSOリポジトリを見てきましたが、それらはすべて通常のjsonファイルを書き込んでおり、私の場合も機能するはずですが、失敗し続けています。
このように単一のファイルを読み取ると、次のように機能します。
data_json = io.open('one.jsonl', mode='r', encoding='utf-8-sig') # Opens in the JSONL file
data_python = extract_json(data_json)
for line in data_python:
print(line)
####outputs####
#{'name': 'one', 'description': 'testDescription...', 'comment': '1'}
#{'name': 'two', 'description': 'testDescription2...', 'comment': '2'}