Search Tuples for Partial String Matches Using the Results of XML Parsing

tayninhbuon

New Member
After several operations, I obtain a list of the pattern: \[code\][(u'DAY1 KWH', u'300.000000'), (u'DAY2 KWH', u'300.000000'), (u'DAY3 KWH', u'300.000000'), (u'DAY4 KWH', u'300.000000'), (u'DAY5 KWH', u'300.000000'), (u'DAY6 KWH', u'300.000000'), (u'DAY7 KWH', u'300.000000'), (u'DAY8 KWH', u'300.000000'), (u'DAY9 KWH', u'300.000000'), (u'DAY10 KWH', u'300.000000'), (u'DAY11 KWH', u'300.000000'), (u'DAY12 KWH', u'300.000000'), (u'DAY13 KWH', u'300.000000'), (u'DAY14 KWH', u'300.000000'),...\[/code\]Not all the elements contain the word "day" or "kwh" - in fact they could be measurements of natural gas use for the month, or water usage for the week, etc. What I would like to do is extract all of the "day" values and place them into one list, all of the "week" values and place them into another list, and all of the "month" values into a third list. The end goal is to be able to graph the daily, weekly, and monthly utility usage. Be aware that these are test values.None of the five different methods I have commented out actually worked, but each attempt did result from reading different Stack Overflow threads. I know the code could definitely be made more efficient/faster, so if you have any optimization suggestions please feel free to add those as well. Your help is greatly appreciated!\[code\]import urllibimport urllib2from xml.dom import minidomimport matplotlib.pyplot as pltdef main(): path = "http://128.226.6.214/bacrest/bacnet_device_70200/" BACrest = 'urn:BACrestService' xlink = 'http://www.w3.org/1999/xlink' dom = minidom.parse(urllib.urlopen(path)) values = [] descriptions = [] for node in dom.getElementsByTagNameNS(BACrest, 'ChildNode'): href = http://stackoverflow.com/questions/10867286/node.getAttributeNS(xlink,'href') descriptionDomain = href + '/Description' descriptionSubDom = minidom.parse(urllib.urlopen(descriptionDomain)) descriptionElements = descriptionSubDom.getElementsByTagNameNS(BACrest, 'return') descriptions.append(descriptionElements) valueDomain = href + '/Value' valueSubDom = minidom.parse(urllib.urlopen(valueDomain)) valueElements = valueSubDom.getElementsByTagNameNS(BACrest, 'return')[0].firstChild.data values.append(valueElements) combination = zip(descriptions,values)\[/code\]Solution Attempts\[code\] #print filter(lambda x: 'DAY1 ' in x, combination) #dayInfo = [] #for sublist in combination: #if 'DAY1 ' in sublist: #dayInfo.append(sublist) #print dayInfo #dayInfo = [s for s in combination if 'DAY1 ' in s] #print dayInfo #dayInfo = [i for i,v in combination if i.startswith('DAY1') in i] #print dayInfo #dayInfo = [] #if any('DAY1 ' in x for x in combination): #dayInfo.append(x) #print dayInfomain()\[/code\]
 
Back
Top