首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >元组:从AttributeError数据中读取时,元组对象没有'read‘属性

元组:从AttributeError数据中读取时,元组对象没有'read‘属性
EN

Stack Overflow用户
提问于 2019-02-22 09:45:07
回答 2查看 1.5K关注 0票数 0

所以这很奇怪。我正在尝试让一个脚本在我的RaspberryPi上运行,以便从weatherunderground的JSON流中获取天气数据。我正在用Python3.5在最新的Raspbian-Stretch OS上开发一个新的Raspberry Pi。它可以在其他机器上运行(windows通过VisualStudio和另一个运行相同发行版的Raspberry Pi和一个Onion Omega2 on LEDES发行版)

我正在阅读的行(编辑自本网站上的其他搜索)是:

代码语言:javascript
复制
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的返回值如下:

代码语言:javascript
复制
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'

我不是一个程序员,只是试图学习,这让我感到困惑,因为它可以在其他系统上工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-22 13:48:10

正如RafaelC所说,您应该使用urlopen。然而,RafaelC的代码有问题。由于我不能对他的答案添加评论,所以我将其作为答案发布。weatherdata.read()返回字节对象而不是字符串,所以我们必须使用.decode()对其进行转换

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2019-02-22 09:46:57

使用urlopen而不是urlretrieve

代码语言:javascript
复制
weatherdata = urllib.request.urlopen("http://api.wunderground.com/api/c8659b546235193f/conditions/q/pws:KKYLOUIS68.json")
json.loads(weatherdata.read())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54818876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档