if statement - python pretty printing simple if -


i have pretty printed content in way using code. code prints out, how print specific location using if ? such upper bukit timah, west coast...

area: upper bukit timah summary: cloudy latitude: 1.356084 longitude: 103.768873

area: west coast summary: cloudy latitude: 1.30039493 longitude: 103.7504196

area: woodlands summary: cloudy latitude: 1.44043052 longitude: 103.7878418

area: yishun summary: cloudy latitude: 1.42738834 longitude: 103.8290405

import urllib2 beautifulsoup import beautifulstonesoup #using bs3   url="https://api.projectnimbus.org/neaodataservice.svc/nowcastset" request = urllib2.request(url) request.add_header("accept", "*/*") request.add_header('accountkey', "osjeroqjtg4v7ec3kiecjw==") request.add_header('uniqueuserid', "00000000000000000000000000000001") result = urllib2.urlopen(request) xml_str = result.read()  soup = beautifulstonesoup(xml_str)  prop_list = [] content in soup.findall("m:properties"):     props = {}     prop in content.findchildren():         props[prop.name[2:]] = prop.text     prop_list.append(props)   prop in sorted(prop_list):     print "area: %(area)s\nsummary: %(summary)s\nlatitude: %(latitude)s\nlongitude: %(longitude)s\n" % prop 

well, you'd have add if statement final for loop, checking whether current entry in in positive list. this:

areas_to_print = ["upper bukit timah", "west coast", "woodlands", "yishun"]  prop in sorted(prop_list):     if prop["area"] in areas_to_print:         print "area: %(area)s\nsummary: %(summary)s\nlatitude: %(latitude)s\nlongitude: %(longitude)s\n" % prop 

alternatively, add same if statement first for loop, entries added prop_list in first place.


Comments