[Python] AttributeError: 'list' object has no attribute 'items' when constructing urllib...

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have looked around the site and I can't find out how to get an output I just keep on getting the same error:

    AttributeError: 'list' object has no attribute 'items'


    I can't figure out why I keep on getting this error any help will be a appreciated!

    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()))


    If needed, here is the full error:

    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'


    EDIT: Code is now as follows

    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()))


    This Code now has the same exact error but the last line is now:

    AttributeError: 'str' object has no attribute 'items'

    Continue reading...

Compartilhe esta Página