我查看了网站,但我无法找到如何获得输出我只是继续得到同样的错误:
AttributeError: 'list' object has no attribute 'items'
我无法弄清楚为什么我会继续得到这个错误,任何帮助都会受到赞赏!
import json
from urllib.request import urlopen, Request
from urllib.parse import urlencode
def sTrackTemperature():
"Constantly Show an Output of the Track Temperature"
sDataRaw = urlopen(Request("https://api.samsara.com/v1/sensors/temperature?access_token=", 518, [2]))
sDataParse = sDataRaw.read().decode('utf-8')
sDataJson = json.loads(sDataParse)
return sDataJson;
print(str(sTrackTemperature()))
如果需要,这是完整的错误:
Traceback (most recent call last):
File "C:/Users/**********/AppData/Local/Programs/Python/Python35-32/Kartplex/TrackTemperature.py", line 17, in <module>
print(str(sTrackTemperature()))
File "C:/Users/**********/AppData/Local/Programs/Python/Python35-32/Kartplex/TrackTemperature.py", line 13, in sTrackTemperature
sDataRaw = urlopen(Request("https://api.samsara.com/v1/sensors/temperature?access_token=**********************", 518, [2]))
File "C:\Users\**********\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 274, in __init__
for key, value in headers.items():
AttributeError: 'list' object has no attribute 'items'
编辑: 代码现在如下
import json
from urllib.request import urlopen, Request
from urllib.parse import urlencode
def sTrackTemperature():
"Constantly Show an Output of the Track Temperature"
SENSOR = {'SENSOR': '2'}
sDataRaw = urlopen(Request("https://api.samsara.com/v1/sensors/temperature?access_token=****************", 518, SENSOR['SENSOR']))
sDataParse = sDataRaw.read().decode('utf-8')
sDataJson = json.loads(sDataParse)
return sDataJson;
print(str(sTrackTemperature()))
此代码现在具有相同的确切错误,但最后一行现在是:
AttributeError: 'str' object has no attribute 'items'
答案 0 :(得分:0)
您只将位置参数传递给Request
构造函数:
Request("https://api.samsara.com/v1/sensors/temperature?access_token=", 518, [2])
鉴于init
Request
的{{1}}签名:documentation):
def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False, method=None)
你正在传递
url
(好!)'518'
为data
(坏:应该是一个字典!)'[2]'
为headers
(不好:应该是一个字典!)嗯,您猜对了:headers
必须是dict
标头名称,标头值对。在__init__
中,调用了标头字典的.items()
方法,该方法适用于您传入的list
。