我不知道如何使用Python3创建JSONL。
test = [{'a': 'b'}, {'a': 'b'}, {'a': 'b'}]
with open("data.json", 'w') as f:
for item in test:
json.dump(item, f)
with open("data.json") as f:
for line in f:
// only 1 line here!
print(line)
// prints
{"a": "b"}{"a": "b"}{"a": "b"}
我尝试使用缩进选项进行转储,但似乎没有什么不同,并且分隔符选项似乎不是一个很好的用例。不知道我在这里想念什么吗?
答案 0 :(得分:1)
使用.write
和换行符\n
例如:
import json
test = [{'a': 'b'}, {'a': 'b'}, {'a': 'b'}]
with open("data.json", 'w') as f:
for item in test:
f.write(json.dumps(item) + "\n")
with open("data.json") as f:
for line in f:
print(line)