raspberry pi 2的代码错误

2024-04-25 06:27:35 发布

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

在获得代码方面的帮助后,仍然会出现一些错误。你知道吗

import urllib.request
import json
r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
rr = str(r)

obj = json.loads(rr)

# filter only the b16 objects
b16_objs = filter(lambda a: a['routeName'] == 'B16',  obj['arrivals'])

if b16_objs:
# get the first item
b16 = b16_objs[0]
my_estimatedWait = b16['estimatedWait']
print(my_estimatedWait)

这是我得到的错误,我不知道如何修复这一点,因为我是python和raspberry pi2的新手。谢谢

File "/usr/lib/python3.2/json/decoder.py", line 369, in raw_decode
obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "program6.py", line 6, in <module>
obj = json.loads(rr)
File "/usr/lib/python3.2/json/__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.2/json/decoder.py", line 353, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.2/json/decoder.py", line 371, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

谢谢你的帮助


Tags: theinpyjsonobjlibusrline
3条回答

您需要解码bytes对象以生成字符串

这会解决你的问题。你知道吗

>>> import urllib.request
>>> import json
>>> r = urllib.request.urlopen("http://www.countdown.tfl.gov.uk/stopBoard/50051").read()
>>> json.loads(r.decode('utf-8'))
{'arrivals': [{'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Erith', 'routeName': 'B12', 'routeId': 'B12'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': 'due', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:37', 'isRealTime': True, 'estimatedWait': '1 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:39', 'isRealTime': True, 'estimatedWait': '2 min', 'destination': 'New Eltham', 'routeName': 'B13', 'routeId': 'B13'}, {'isCancelled': False, 'scheduledTime': '08:45', 'isRealTime': True, 'estimatedWait': '8 min', 'destination': 'Kidbrooke', 'routeName': 'B16', 'routeId': 'B16'}, {'isCancelled': False, 'scheduledTime': '08:48', 'isRealTime': True, 'estimatedWait': '11 min', 'destination': 'North Greenwich', 'routeName': '486', 'routeId': '486'}, {'isCancelled': False, 'scheduledTime': '08:51', 'isRealTime': True, 'estimatedWait': '14 min', 'destination': 'Woolwich', 'routeName': '96', 'routeId': '96'}, {'isCancelled': False, 'scheduledTime': '08:55', 'isRealTime': True, 'estimatedWait': '19 min', 'destination': 'Horn Park', 'routeName': 'B15', 'routeId': 'B15'}, {'isCancelled': False, 'scheduledTime': '08:57', 'isRealTime': True, 'estimatedWait': '20 min', 'destination': 'Lewisham Centre', 'routeName': '89', 'routeId': '89'}, {'isCancelled': False, 'scheduledTime': '08:58', 'isRealTime': True, 'estimatedWait': '22 min', 'destination': 'North Greenwich', 'routeName': '422', 'routeId': '422'}], 'lastUpdated': '09:36', 'filterOut': [], 'serviceDisruptions': {'infoMessages': [], 'criticalMessages': [], 'importantMessages': []}}

将行rr = str(r)替换为:

rr = r.decode()

我认为luoluo答案应该能解决你的问题。不过,我认为您需要看看^{}库,因为它内置了json decoder

您的代码使用请求:

import requests
obj  = requests.get("http://www.countdown.tfl.gov.uk/stopBoard/50051").json()

b16_objs = list(filter(lambda a: a['routeName'] == 'B16',  obj['arrivals']))                                                                           

if b16_objs:
    estimated_wait = b16_objs[0]['estimatedWait']
    print(estimated_wait)

相关问题 更多 >