【问题标题】:merge & write two jsonl (json lines) files into a new jsonl file in python3.6在python3.6中将两个jsonl(json行)文件合并并写入一个新的jsonl文件
【发布时间】:2020-09-14 20:47:20
【问题描述】:

您好,我有两个 jsonl 文件,如下所示:

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'}

【问题讨论】:

  • 能把extract_json函数的内容贴出来吗?
  • 嘿老板,刚刚更新了我的输出。
  • 您希望输出为 jsonl 格式还是纯 json 格式?
  • 是的,请使用 jsonl 格式。我将不得不共享此文件,因此其他方不必进行清洁。可能吗?非常感谢您的帮助。

标签: python json merge jsonlines


【解决方案1】:

extract_json 有可能返回一个生成器,而不是一个可序列化 json 的列表/字典
因为它是 jsonl,这意味着每一行都是一个有效的 json
所以你只需要稍微调整你现有的代码。

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:
        for line in infile.readlines():
            try:
                result.append(json.loads(line)) # read each line of the file
            except ValueError:
                print(f)

# This would output jsonl
with open('merged_file.jsonl','w', encoding= 'utf-8-sig') as outfile:
    #json.dump(result, outfile)
    #write each line as a json
    outfile.write("\n".join(map(json.dumps, result)))

现在我想起来你甚至不必使用 json 来加载它,除了它会帮助你清理任何格式错误的 JSON 行

你可以像这样在一个镜头中收集所有的线条

outfile = open('merged_file.jsonl','w', encoding= 'utf-8-sig')
for f in glob.glob("folder_with_all_jsonl/*.jsonl"):
    with open(f, 'r', encoding='utf-8-sig') as infile:
        for line in infile.readlines():
            outfile.write(line)
outfile.close()

【讨论】:

  • 谢谢伙计。是否可以像jsonl格式一样写,也没有[]环绕?
  • 有效!!谢谢你,先生。感谢您抽出宝贵时间帮助我:)保重!
【解决方案2】:

另一种超级简单的方法,如果您不关心 json 验证

cat folder_with_all_jsonl/*.jsonl > merged_file.jsonl

【讨论】:

  • 我实际上是要建议这个的。你打败了我,该死!
  • 哇,太好了。虽然我不擅长控制台命令,但这看起来很不错。谢谢你,伙计:) 谢谢@UltaPitt 的意见,呵呵
猜你喜欢
  • 2021-11-22
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 2021-08-13
  • 2016-05-14
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多