import jsonlines
# 读取一个.jsonl文件并将其内容转换为Python列表
def read_jsonl(path):
content = []
with jsonlines.open(path, "r") as json_file:
for obj in json_file.iter(type=dict, skip_invalid=True):
content.append(obj)
return content
# 将一个Python列表的内容写入到一个.jsonl文件中
def write_jsonl(path, content):
with jsonlines.open(path, "w") as json_file:
json_file.write_all(content)
jsonl文件无法直接处理,因此我们在python中需要将其转换为列表,第一个函数read_jsonl可以将jsonl文件转化为python列表,第二个函数write_jsonl文件可以将python列表转化为jsonl文件。