为了账号安全,请及时绑定邮箱和手机立即绑定

python程序读取输出错误的json文件中的特定字段

python程序读取输出错误的json文件中的特定字段

饮歌长啸 2021-12-16 16:08:41
所以我有以下代码import jsonwith open("output.json") as f:    data = json.loads(f.read())    print(data[0]['url'])问题是当我尝试给它一个要处理的文件时出现以下错误Traceback (most recent call last):  File "C:/Users/pedre/PycharmProjects/app_pdi/app_pdi.py", line 6, in <module>    data = json.loads(f.read())  File "C:\Users\pedre\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 348, in loads    return _default_decoder.decode(s)  File "C:\Users\pedre\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py", line 340, in decode    raise JSONDecodeError("Extra data", s, end)json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 408)我正在使用 PyCharm 编写我的代码,有人可以帮助我吗?我对 Python 很陌生,所以请善待 xD编辑1:我的 JSON 文件具有以下结构:{"urlkey": "pt,gov,70ja)/", "timestamp": "20190221014307", "url": "http://70ja.gov.pt/", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247497858.46/warc/CC-MAIN-20190221010932-20190221032932-00158.warc.gz", "mime-detected": "text/html", "offset": "4514453", "digest": "LAOPRAYYQUABW3B4ISZINPKETGB4MYRG", "languages": "por", "status": "200", "length": "22629", "mime": "text/html", "charset": "UTF-8"}{"urlkey": "pt,gov,70ja)/robots.txt", "timestamp": "20190221024013", "url": "http://70ja.gov.pt/robots.txt", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247497858.46/robotstxt/CC-MAIN-20190221010932-20190221032932-00489.warc.gz", "mime-detected": "text/plain", "offset": "23166", "digest": "5S2OB6LK7EHO74WNBNVLNE6ZBB5VRYTI", "status": "200", "length": "818", "mime": "text/plain"}{"urlkey": "pt,gov,academiaportuguesadahistoria)/", "timestamp": "20190218105706", "url": "https://academiaportuguesadahistoria.gov.pt/", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247484928.52/warc/CC-MAIN-20190218094308-20190218120308-00509.warc.gz", "mime-detected": "text/html", "offset": "498335094", "digest": "EQOJY7DY2YL752S6QCRXLGNDTELPYLD3", "languages": "por", "status": "200", "length": "10240", "mime": "text/html", "charset": "UTF-8"}
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

Python 将 JSON 文本解析为字典。这里是完整的答案。


您的 JSON 文件可能如下所示:


[

    {"urlkey": "pt,gov,70ja)/", "timestamp": "20190221014307", "url": "http://70ja.gov.pt/", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247497858.46/warc/CC-MAIN-20190221010932-20190221032932-00158.warc.gz", "mime-detected": "text/html", "offset": "4514453", "digest": "LAOPRAYYQUABW3B4ISZINPKETGB4MYRG", "languages": "por", "status": "200", "length": "22629", "mime": "text/html", "charset": "UTF-8"},

    {"urlkey": "pt,gov,70ja)/robots.txt", "timestamp": "20190221024013", "url": "http://70ja.gov.pt/robots.txt", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247497858.46/robotstxt/CC-MAIN-20190221010932-20190221032932-00489.warc.gz", "mime-detected": "text/plain", "offset": "23166", "digest": "5S2OB6LK7EHO74WNBNVLNE6ZBB5VRYTI", "status": "200", "length": "818", "mime": "text/plain"},

    {"urlkey": "pt,gov,academiaportuguesadahistoria)/", "timestamp": "20190218105706", "url": "https://academiaportuguesadahistoria.gov.pt/", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247484928.52/warc/CC-MAIN-20190218094308-20190218120308-00509.warc.gz", "mime-detected": "text/html", "offset": "498335094", "digest": "EQOJY7DY2YL752S6QCRXLGNDTELPYLD3", "languages": "por", "status": "200", "length": "10240", "mime": "text/html", "charset": "UTF-8"}

]

在python3中解析这样一个JSON文件:


import json


with open("jsonDataFile.json") as f:

    jsonData = json.loads(f.read())

    for array in jsonData:

        for key, value in array.items():

            print("Key:%s Value:%s" % (key,value))

如果您的文件不是 json 数据数组:


{"urlkey": "pt,gov,70ja)/", "timestamp": "20190221014307", "url": "http://70ja.gov.pt/", "filename": "crawl-data/CC-MAIN-2019-09/segments/1550247497858.46/warc/CC-MAIN-20190221010932-20190221032932-00158.warc.gz", "mime-detected": "text/html", "offset": "4514453", "digest": "LAOPRAYYQUABW3B4ISZINPKETGB4MYRG", "languages": "por", "status": "200", "length": "22629", "mime": "text/html", "charset": "UTF-8"}

在python3中解析这样一个JSON文件:


import json


with open("test3.json") as f:

    jsonData = json.loads(f.read())


    for key, value in jsonData.items():

        print("Key:%s Value:%s" % (key,value))


查看完整回答
反对 回复 2021-12-16
  • 1 回答
  • 0 关注
  • 564 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号