所以我有以下代码
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"}
Python将JSON文本解析为一个字典
您的JSON文件可以如下所示:
在python3中解析这样一个JSON文件:
如果文件不是json数据数组:
在python3中解析这样一个JSON文件:
相关问题 更多 >
编程相关推荐