blu*_*nox 20 python json jsonlines
我想在 python 中加载一个 JSONL 文件作为 JSON 对象。有没有简单的方法来做到这一点?
cry*_*bhu 33
完整的步骤,包括像我这样的初学者的文件操作
假设你有一个.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"}
Run Code Online (Sandbox Code Playgroud)
此代码应该工作:
import json
with open('./data/my_filename.jsonl', 'r') as json_file:
json_list = list(json_file)
for json_str in json_list:
result = json.loads(json_str)
print(f"result: {result}")
print(isinstance(result, dict))
Run Code Online (Sandbox Code Playgroud)
关于.jsonl
文件:http :
//jsonlines.org/
And*_*yko 16
该splitlines将解决这一问题给你,所以一般情况下面的代码会为你工作:
import json
result = [json.loads(jline) for jline in jsonl_content.splitlines()]
Run Code Online (Sandbox Code Playgroud)
如果这是响应对象,结果将是:
result = [json.loads(jline) for jline in response.read().splitlines()]
Run Code Online (Sandbox Code Playgroud)
小智 6
将参数行设置为 True 应该可以解决问题。
import pandas as pd
jsonObj = pd.read_json(path_or_buf=file_path, lines=True)
Run Code Online (Sandbox Code Playgroud)
无需使用任何split()
功能即可快速简便的原生解决方案:
import json
with open('/path/to/file.jsonl') as f:
data = [json.loads(line) for line in f]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29698 次 |
最近记录: |