我想在python中加载JSONL文件作为JSON对象。有没有简单的方法呢?
答案 0 :(得分:4)
无需使用任何 split()
函数即可快速简便的本机解决方案:
import json
with open('/path/to/file.jsonl') as f:
data = [json.loads(line) for line in f]
答案 1 :(得分:2)
一般来说,以下代码适合您:
import json
result = [json.loads(jline) for jline in jsonl_content.split('\n')]
如果那是响应对象,结果将是:
result = [json.loads(jline) for jline in response.read().split('\n')]
答案 2 :(得分:2)
完整的步骤,包括像我这样的初学者的文件操作
假设您有一个.jsonl
文件,例如:
{"reviewerID": "A2IBPI20UZIR0U", "asin": "1384719342", "reviewerName": "cassandra tu \"Yeah, well, that's just like, u...", "helpful": [0, 0], "reviewText": "Not much to write about here, but it does exactly what it's supposed to. filters out the pop sounds. now my recordings are much more crisp. it is one of the lowest prices pop filters on amazon so might as well buy it, they honestly work the same despite their pricing,", "overall": 5.0, "summary": "good", "unixReviewTime": 1393545600, "reviewTime": "02 28, 2014"}
{"reviewerID": "A14VAT5EAX3D9S", "asin": "1384719342", "reviewerName": "Jake", "helpful": [13, 14], "reviewText": "The product does exactly as it should and is quite affordable.I did not realized it was double screened until it arrived, so it was even better than I had expected.As an added bonus, one of the screens carries a small hint of the smell of an old grape candy I used to buy, so for reminiscent's sake, I cannot stop putting the pop filter next to my nose and smelling it after recording. :DIf you needed a pop filter, this will work just as well as the expensive ones, and it may even come with a pleasing aroma like mine did!Buy this product! :]", "overall": 5.0, "summary": "Jake", "unixReviewTime": 1363392000, "reviewTime": "03 16, 2013"}
此代码应该可以工作:
import json
with open('./data/my_filename.json', 'r') as json_file:
json_list = list(json_file)
for json_str in json_list:
result = json.loads(json_str)
print("result: {}".format(result))
print(isinstance(result, dict))
关于.jsonl
个文件:
http://jsonlines.org/
答案 3 :(得分:0)
将参数行设置为True应该可以解决问题。
import pandas as pd
jsonObj = pd.read_json(path_or_buf=file_path, lines=True)
答案 4 :(得分:0)
您可以添加更多密钥,但这应该可以。说,每一行都是以下格式。基本上,j_line 是一个字典,可以像访问字典一样访问每个元素。我也共享访问嵌套对象。
{"key1":"value", "key2":{"prop_1": "value"}}
with open("foo.jsonl") as f1:
for line in f1:
j_line=json.loads(line)
key_1=j_line['key1']
prop_1=j_line['key2']['prop_2]