8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

将 JSONL 文件加载为 JSON 对象

Patrik Holub 1月前

40 0

我想在 Python 中将 JSONL 文件加载为 JSON 对象。有没有简单的方法可以做到这一点?

我想在 Python 中将 JSONL 文件加载为 JSON 对象。有没有简单的方法可以做到这一点?

帖子版权声明 1、本帖标题:将 JSONL 文件加载为 JSON 对象
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Patrik Holub在本站《csv》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您可以添加更多键,但这应该可行。假设每行都采用以下格式。基本上,j_line 是一个字典,可以像访问字典一样访问每个元素。我也分享了访问嵌套对象。

    {\'key1\':\'值\', \'key2\':{\'prop_1\':\'值\'}}

    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]
    
  • 无需使用任何功能的快速简便的本机解决方案 split()

    import json
    with open('/path/to/file.jsonl') as f:
        data = [json.loads(line) for line in f]
    
  • 完整的步骤,包括适合像我这样的初学者的文件操作

    假设您有一个 .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.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))
    

    关于 .jsonl 文件:
    http://jsonlines.org/

  • 将参数线设置为 True 就可以了。

    import pandas as pd    
    jsonObj = pd.read_json(path_or_buf=file_path, lines=True)
    
  • @CMCDragonkai 字符串中的新行从 \n -> \\n 序列化,这不被视为换行符。因此,以 json 字符串表示的对象的换行符被保留。

  • dam 1月前 0 只看Ta
    引用 7

    splitlines 如何覆盖该内容?如果 JSON 对象内部有换行符,则该换行符会在该点处被拆分。

  • 分割 splitlines 可以为您解决这个问题,因此一般来说,下面的代码适合您:

    import json
    
    result = [json.loads(jline) for jline in jsonl_content.splitlines()]
    

    如果这是响应对象,则结果将是:

    result = [json.loads(jline) for jline in response.read().splitlines()]
    
  • 谢谢你的评论。我以前真的没有用过这些格式,显然我并没有真正理解 JSONL 的意义——你的评论帮助我理解了!实际上给出的答案对我来说仍然有用——所以我编辑了这个问题,让它有意义。再次感谢你的评论!

返回
作者最近主题: