AttributeError: 'set' object has no attribute 'items - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: AttributeError: 'set' object has no attribute 'items (/thread-9817.html) |
AttributeError: 'set' object has no attribute 'items - hey_arnold - Apr-29-2018 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) RE: AttributeError: 'set' object has no attribute 'items - killerrex - Apr-29-2018 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... RE: AttributeError: 'set' object has no attribute 'items - j.crater - Apr-29-2018 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. RE: AttributeError: 'set' object has no attribute 'items - hey_arnold - Apr-29-2018 Thanks :) |