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

2024-04-24 23:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有以下代码

import json

with 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"}

Tags: textptjsonurldatamainhtmlline
1条回答
网友
1楼 · 发布于 2024-04-24 23:38:38

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))

相关问题 更多 >