python jsonl格式文件转为json格式文件

jsonl文件格式示例:

{'ab8145f6d1ee0d7a7a1a2730d50105c0645c21e663767754ef5eba85cf41ddc1': 'Stadacona.'}
{'3a72ecafa9cc526ec3a44a6230aaa7e243e319d2212015b207582ae9c65f9705': 'Stadacona.'}

需要转化为如下格式json文件:
格式1:

{
  "ab8145f6d1ee0d7a7a1a2730d50105c0645c21e663767754ef5eba85cf41ddc1": "Stadacona.",
  "3a72ecafa9cc526ec3a44a6230aaa7e243e319d2212015b207582ae9c65f9705": "Stadacona."
}

格式2:

[
    {
        "ab8145f6d1ee0d7a7a1a2730d50105c0645c21e663767754ef5eba85cf41ddc1": "Stadacona."
    },
    {
        "3a72ecafa9cc526ec3a44a6230aaa7e243e319d2212015b207582ae9c65f9705": "Stadacona."
    }
]

转化为格式1的代码:(需要注意的是,读取文件和写入文件的encoding需要指定为一致,不然会导致gbk和utf混用,导致评测和微调的时候出现字符错误)

import json

def jsonl_to_json(jsonl_file, json_file):
    with open(jsonl_file, 'r', encoding='utf-8') as f:
        jsonl_data = f.readlines()
    # print(jsonl_data)
    # print(jsonl_data[0])
    # print(jsonl_data[0][1:-2])
    # print(jsonl_data[14])

    with open(json_file, 'w', encoding='utf-8') as f:
        for line in jsonl_data:
            line = line[1:-2]
            f.write('    ' + line + ',' + '\n')
    f.close()


jsonl_file = 'C:/Users/Desktop/1.jsonl'
json_file = 'C:/Users/Desktop/test.json'
jsonl_to_json(jsonl_file, json_file)

如果json文件的anwer有多个答案,如:

{'afec84fa0ca103df7b548b9d6a9c33d916ee6464f6585b836bb076e4cdeb8d0b': 'Great Lakes, Pacific Ocean, Atlantic Ocean.'}
{'f492297aef364879271558f8f334d261b4c513357f52bd3c89265d30865eb03f': 'Candy cane, Sprint car racing, ice cream cone, Thanksgiving.'}

那么需要用如下代码转换:

import json

def jsonl_to_json(jsonl_file, json_file):
    converted_items = {}
    with open(jsonl_file, 'r', encoding='utf-8') as f:
        for line in f:
            try:
                item = eval(line)
            except json.decoder.JSONDecodeError:
                print(line)
                return
            pid = list(item.keys())[0]
            print(pid)
            answer = item[pid]
            answers = answer.replace(".", "").split(", ")
            converted_items[pid] = answers

    with open(json_file, 'w', encoding='utf-8') as f:
        json.dump(converted_items, f)


jsonl_file = 'C:/Users/Desktop/epoch_13_KoRC_outputs.jsonl'
json_file = 'C:/Users/Desktop/iid.json'
jsonl_to_json(jsonl_file, json_file)

转化为格式2的代码:(注意需要将jsonl的’转化为”)

import json

def jsonl_to_json(jsonl_file, json_file):
    json_data = []
    with open(jsonl_file, 'r', encoding='utf-8') as f:
        for line in f:
            try:
                obj = json.loads(line)
                json_data.append(obj)
            except json.JSONDecodeError:
                print(f"Ignoring invalid JSON object: {line}")

    with open(json_file, 'w', encoding='utf-8') as f:
        json.dump(json_data, f, indent=4)

jsonl_file = 'C:/Users/Desktop/1.jsonl'
json_file = 'C:/Users/Desktop/test.json'
jsonl_to_json(jsonl_file, json_file)

你可能感兴趣的:(数据清洗,python,json)