【问题标题】:Merge multiple JSONL files from a folder using Python使用 Python 合并文件夹中的多个 JSONL 文件
【发布时间】:2021-11-22 23:44:45
【问题描述】:

我正在寻找一种解决方案,使用 Python 脚本从一个文件夹中合并多个 JSONL 文件。下面的脚本适用于 JSON 文件。

import json
import glob

result = []
for f in glob.glob("*.json"):
    with jsonlines.open(f) as infile:
        result.append(json.load(infile))

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

请在下面找到我的 JSONL 文件示例(只有一行):

{"date":"2021-01-02T08:40:11.378000000Z","partitionId":"0","sequenceNumber":"4636458","offset":"1327163410568","iotHubDate":"2021-01-02T08:40:11.258000000Z","iotDeviceId":"text","iotMsg":{"header":{"deviceTokenJwt":"text","msgType":"text","msgOffset":3848,"msgKey":"text","msgCreation":"2021-01-02T09:40:03.961+01:00","appName":"text","appVersion":"text","customerType":"text","customerGroup":"Customer"},"msgData":{"serialNumber":"text","machineComponentTypeId":"text","applicationVersion":"3.1.4","bootloaderVersion":"text","firstConnectionDate":"2018-02-20T10:34:47+01:00","lastConnectionDate":"2020-12-31T12:05:04.113+01:00","counters":[{"type":"DurationCounter","id":"text","value":"text"},{"type":"DurationCounter","id":"text","value":"text"},{"type":"DurationCounter","id":"text","value":"text"},{"type":"IntegerCounter","id":"text","value":2423},{"type":"IntegerCounter","id":"text","value":9914},{"type":"DurationCounter","id":"text","value":"text"},{"type":"IntegerCounter","id":"text","value":976},{"type":"DurationCounter","id":"text","value":"PT0S"},{"type":"IntegerCounter","id":"text","value":28},{"type":"DurationCounter","id":"text","value":"PT0S"},{"type":"DurationCounter","id":"text","value":"PT0S"},{"type":"DurationCounter","id":"text","value":"text"},{"type":"IntegerCounter","id":"text","value":1}],"defects":[{"description":"ProtocolDb.ProtocolIdNotFound","defectLevelId":"Warning","occurrence":3},{"description":"BridgeBus.CrcError","defectLevelId":"Warning","occurrence":1},{"description":"BridgeBus.Disconnected","defectLevelId":"Warning","occurrence":6}],"maintenanceEvents":[{"interventionId":"Other","comment":"text","appearance_display":0,"intervention_date":"2018-11-29T09:52:16.726+01:00","intervention_counterValue":"text","intervention_workerName":"text"},{"interventionId":"Other","comment":"text","appearance_display":0,"intervention_date":"2019-06-04T15:30:15.954+02:00","intervention_counterValue":"text","intervention_workerName":"text"}]}}}

有人知道我该如何处理这个加载吗?

【问题讨论】:

    标签: python json merge etl jsonlines


    【解决方案1】:

    您可以使用您加载的每个 json 对象更新主字典。喜欢

    import json
    import glob
    
    result = {}
    for f in glob.glob("*.json"):
        with jsonlines.open(f) as infile:
            result.update(json.load(infile)) #merge the dicts
    
    with open("merged_file.json", "wb") as outfile:
         json.dump(result, outfile)
    

    但这会覆盖相似的键。!

    【讨论】:

    • 当我运行你的代码时,我得到这个错误:EOFError
    【解决方案2】:

    由于 JSONL 文件中的每一行都是一个完整的 JSON 对象,因此您实际上根本不需要解析 JSONL 文件即可将它们合并到另一个 JSONL 文件中。相反,只需将它们连接起来即可合并它们。但是,这里需要注意的是 JSONL 格式不要求文件末尾有换行符。因此,您必须将每一行读入缓冲区以测试 JSONL 文件是否在没有换行符的情况下结束,在这种情况下,您必须显式输出换行符以分隔下一个文件的第一条记录:

    with open("merged_file.json", "w") as outfile:
        for filename in glob.glob("*.json"):
            with open(filename) as infile:
                for line in infile:
                    outfile.write(line)
                if not line.endswith('\n'):
                    outfile.write('\n')
    

    【讨论】:

    • 感谢@blhsing 的回答,但不幸的是我没有得到正确的数据(缺少行)!
    • 请使用您的输入文件样本更新问题。
    • 我喜欢这个答案,但如果任何 jsonl 文件末尾缺少换行符,您将丢失行。您可能需要逐行处理以查看那里是否有最终换行符。
    • @tdelaney 啊谢谢。我没有意识到 JSONL 允许文件在没有换行符的情况下结束。然后相应地更新了答案。
    • @Arvind 除非我误解了 OP 的问题,否则我认为 OP 的意思是将 JSONL 文件合并到另一个 JSONL 文件中,而不是合并到 JSON 文件中。
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2023-01-18
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多