[英]python program to read a specific field in json file outpouting erros
so i have the following code所以我有以下代码
import json
with open("output.json") as f:
data = json.loads(f.read())
print(data[0]['url'])
the thing is i get the following errors when i try to give it a file to process问题是当我尝试给它一个要处理的文件时出现以下错误
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)
i am using PyCharm to write my code, can some one help me ?我正在使用 PyCharm 编写我的代码,有人可以帮助我吗? i am quite new to Python so please be nice xD
我对 Python 很陌生,所以请善待 xD
EDIT1:编辑1:
my JSON file has the following structure:我的 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 parses JSON text to a dictionary.Here is complete answer. Python 将 JSON 文本解析为字典。这里是完整的答案。
Your JSON file can be like below:您的 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"}
]
Parsing such a JSON file in python3:在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))
If your file is not an array of json data:如果您的文件不是 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"}
Parsing such a JSON file in python3:在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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.