所以这很奇怪。我正在尝试让一个脚本在我的RaspberryPi上运行,以便从weatherunderground的JSON流中获取天气数据。我正在用Python3.5在最新的Raspbian-Stretch OS上开发一个新的Raspberry Pi。它可以在其他机器上运行(windows通过VisualStudio和另一个运行相同发行版的Raspberry Pi和一个Onion Omega2 on LEDES发行版)
我正在阅读的行(编辑自本网站上的其他搜索)是:
import urllib.request
import json
# Get and load the weather data from my house weather station.
weatherdata = urllib.request.urlretrieve("http://api.wunderground.com/api/<myAPIKey-hidden here>/conditions/q/pws:KKYLOUIS68.json")
weatherinfo = json.loads(weatherdata.read())shell的返回值如下:
Traceback (most recent call last):
File "/home/pi/myweather_win.py", line 18, in <module>
weatherinfo = json.loads(weatherdata.read())
AttributeError: 'tuple' object has no attribute 'read'我不是一个程序员,只是试图学习,这让我感到困惑,因为它可以在其他系统上工作。
发布于 2019-02-22 13:48:10
正如RafaelC所说,您应该使用urlopen。然而,RafaelC的代码有问题。由于我不能对他的答案添加评论,所以我将其作为答案发布。weatherdata.read()返回字节对象而不是字符串,所以我们必须使用.decode()对其进行转换
weatherdata = urllib.request.urlopen("http://api.wunderground.com/api/c8659b546235193f/conditions/q/pws:KKYLOUIS68.json")
content = weatherdata.read()
json.loads(content.decode())测试平台: Python 3.4.9,CentOS 7.6.1810
发布于 2019-02-22 09:46:57
使用urlopen而不是urlretrieve
weatherdata = urllib.request.urlopen("http://api.wunderground.com/api/c8659b546235193f/conditions/q/pws:KKYLOUIS68.json")
json.loads(weatherdata.read())https://stackoverflow.com/questions/54818876
复制相似问题