Python Forum
AttributeError: 'set' object has no attribute 'items
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'set' object has no attribute 'items
#1
Hi All,

I am having problems trying to work out what I have done wrong, any help would be much appreciated :)

The error points to:

for header in headers.items():

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

 
import requests
import pandas as pd
from lxml import etree

def getXML(toDate, fromDate, dayType):
    url ="http://marketinformation.natgrid.co.uk/MIPIws-public/public/publicwebservice.asmx"
    headers = {'Content-Type: application/soap+xml; charset=utf-8'}
    
    body ="""
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
        <soap12:Body>
            <GetPublicationDataWMResponse xmlns="http://www.NationalGrid.com/MIPI/">
                <reqObject>
                    <LatestFlag>N</LatestFlag>
                    <ApplicableForFlag>Y</ApplicableForFlag>
                    <ToDate>%s</ToDate>
                    <FromDate>%s</FromDate>
                    <DateType>%s</DateType>
                    <PublicationObjectNameList>
                        <string>LNG Stock Level</string>
                    </PublicationObjectNameList>
                </reqObject>
            </GetPublicationDataWM>
        </soap12:Body>
    </soap12:Envelope>
      """% (toDate, fromDate, dayType)

    response = requests.post(url, data=body, headers=headers)

    return response.content

df = pd.DataFrame(columns=("applicable_at","applicable_for","name","value","generated","quality_indicator","substituted","created_date"))

for pd_date in pd.date_range('2016-03-14', '2016-03-15'):
    day = pd_date.strftime('%Y-%m-%d')
    
    root = etree.fromstring(getXML(day,day,"gasday"))
    
    #map prefix 'd' to the default namespace URI
    ns = {'d': 'http://www.NationalGrid.com/MIPI/'}
    
    publication_objects = root.xpath('//d:CLSMIPIPublicationObjectBE', namespaces=ns)
    
    for obj in publication_objects:
        name = obj.find('d:PublicationObjectName', ns).text
        data = obj.find('d:PublicationObjectData/d:CLSMIPIPublicationObjectBE', ns)
        applicable_at = pd.to_datetime(data.find('d:ApplicableFor', ns).text, format ='%Y-%m-%dT%H:%M:%SZ')
        applicable_for = pd.to_datetime(data.find('d:ApplicableFor', ns).text, format ='%Y-%m-%dT%H:%M:%SZ')
        value = float(data.find('d:Value', ns).text)
        generated = pd.to_datatime(data.find('d:GeneratedTimeStamp', ns).text, format ='%Y-%m-%dT%H:%M:%SZ')
        quality_indicator = data.find('d:Value', ns).text
        substituted = data.find('d:Substituted', ns).text
        created_date = pd.to_datetime(data.find('d:CreatedDate', ns).text, format ='%Y-%m-%dT%H:%M:%SZ')
        
        df.loc[len(df) +1] = [applicable_at, applicable_for,name, value, generated, quality_indicator, substituted, created_date]
        
df.head(10)
Reply
#2
The line 7:
(Apr-29-2018, 03:31 PM)hey_arnold Wrote: headers = {'Content-Type: application/soap+xml; charset=utf-8'}

Is a set, not a dictionary as it shall be. I tried with:
headers = {'Content-Type': 'application/soap+xml', 'charset': 'utf-8'}
and does not produce an error, but I do not know if it what you want...
Reply
#3
Hello, the headers argument should be a dictionary, but you assigned the headers variable to a set with a single string argument in line 7.

Check this tutorial, in particular "Working with headers" section.
Reply
#4
Thanks :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "list object has no attribute transpose()" usercat123 4 4,187 Jan-28-2022, 12:01 PM
Last Post: usercat123
  AttributeError: (“module 'pandas' has no attribute 'rolling_std'” Mariana136 4 7,558 Sep-23-2019, 12:56 PM
Last Post: Mariana136
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 45,278 May-29-2019, 02:48 AM
Last Post: heiner55
  Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv' NSearch 9 16,763 Apr-22-2019, 05:05 PM
Last Post: Yoriz
  AttributeError: 'NoneType' object has no attribute 'all' synthex 2 5,208 Mar-07-2019, 11:11 AM
Last Post: synthex
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 3,723 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,483 Nov-04-2018, 10:38 PM
Last Post: stullis
  AttributeError: Can't get attribute 'Individual' on <module 'deap.creator' DomClout 4 8,725 Jul-27-2018, 09:05 PM
Last Post: Vysero
  AttributeError: module 'mnist' has no attribute 'train_images' pythonbeginner 1 8,144 Jun-14-2018, 09:29 PM
Last Post: snippsat
  AttributeError: module 'plotly' has no attribute 'offline' charlesczc 8 16,998 Jan-21-2018, 08:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020