【问题标题】:json.decoder.JSONDecodeError When reading from a filejson.decoder.JSONDecodeError 从文件读取时
【发布时间】:2022-01-24 18:46:29
【问题描述】:

您好,我正在尝试从文件中逐行读取数据,然后将该数据解析为 json。 每一行都是这样的

{"response":"OK","metadata_uri":"ipfs://bafkreicdoxeenpnlli52y6vhmx4tjis6ffelkzq5ujojyo22es2qbomfpq","name":"Happy Sad or Just Fine #14","description":"Perfect for you if you are happy sad or just fine.","file_url":"https://ipfs.io/ipfs/bafkreigcg6g5i47g3sc26lgk7h3zswtxma7qgs55lz4yyfiry3qgatgad4","external_url":null,"animation_url":null,"custom_fields":{"dna":"7b1a7e87d74ffd423c1ac59be1deace94916c6b9","edition":14,"date":1640190624930,"compiler":"HashLips Art Engine"},"attributes":[{"trait_type":"Backgrounds","value":"yellow ","max_value":null,"display_type":null},{"trait_type":"face","value":"red ","max_value":null,"display_type":null},{"trait_type":"eyes","value":"eyes crossed ","max_value":null,"display_type":null},{"trait_type":"mouth","value":"smirt right","max_value":null,"display_type":null}],"error":null}

但是,即使它都是有效的 json,我也会收到错误

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

这是我的代码

import json

f = open(r"C:\Users\wille\Desktop\code\cool_things\hashlips_art_engine-1.1.2_patch_v1\build\json/responce.text","r")

for i in range(1,1000):
    j = f.readline(i)
    j = json.loads(j)
    
    
    print(j)

有谁知道如何解决这个问题或者是什么原因造成的

【问题讨论】:

    标签: python json


    【解决方案1】:

    这不是您使用readline 的方式。 readline(1) 说“从文件中读取最多一个字节”,这当然会搞砸你的 JSON。试试这个:

    import json
    
    f = open(r"C:\Users\wille\Desktop\code\cool_things\hashlips_art_engine-1.1.2_patch_v1\build\json/responce.text","r")
    
    for line in f:
        j = json.loads(line)
        print(j)
    

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    相关资源
    最近更新 更多