1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Why do I receive - AttributeError: 'str' object has no attribute 'items' [closed]

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 14:12.

  1. Stack

    Stack Membro Participativo

    My Code:

    def API_Gather():

    # Build list of Owned Stock Symbols
    self.ticker_List = []

    # Open the data file to find individual symbols owned (self.filePath4)
    with open(self.filePath4, "r") as f:
    self.data = json.load(f)
    f.close()

    # Parse data to gather Owned Ticker Symbols
    for x in self.data:
    data_symbol = x["Symbol"]
    if data_symbol not in self.ticker_List: self.ticker_List.append(data_symbol)
    else:
    next
    next
    my_url = self.API_url
    my_headers = self.API_headers

    for ticker in self.ticker_List:
    my_querystring = {"symbols":ticker}
    # Create the API response
    response = requests.request("GET", url=my_url, headers=my_headers, params=my_querystring)

    # Create the API Data in json format
    data = json.loads(response.content)

    # Bypass header detail
    results = data['quoteResponse']['result']
    self.Symbol = results[0]["symbol"]
    self.Name = results[0]["displayName"]


    My Error Msg:

    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tkinter/__init__.py", line 1962, in __call__
    return self.func(*args)
    ^^^^^^^^^^^^^^^^
    File "/Users/terrylengel/Stocks copy/Stocks5.1.py", line 2388, in Total_PL
    API_Gather()
    File "/Users/terrylengel/Stocks copy/Stocks5.1.py", line 2254, in API_Gather
    response = requests.request("GET", url=my_url, headers=my_headers, params=my_querystring)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/sessions.py", line 575, in request
    prep = self.prepare_request(req)
    ^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/sessions.py", line 484, in prepare_request
    p.prepare(
    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/models.py", line 368, in prepare
    self.prepare_headers(headers)
    File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/requests/models.py", line 488, in prepare_headers
    for header in headers.items():
    ^^^^^^^^^^^^^
    AttributeError: 'str' object has no attribute 'items'


    Note: my_url and my_headers are exact. If run in test code work fine:

    Continue reading...

Compartilhe esta Página