Reputation: 2687
Trying to write a script that will (eventually) color in a US map red or blue depending on if that state's governor is a Republican or Democrat. Before I attempt to actually color the map though, I need to get the 'r' or 'd' value for a given state from dict 'states'. Basically, I'm having trouble matching the string of text that returns from stateid = (p['id'])
to the value of the key of the same name in the dict states
. Can anyone help me figure out how to do this?
import BeautifulSoup
states = {'AL':'r', 'AK':'d', 'AZ':'r', 'AR':'r', 'CA':'d', 'CO':'d', 'CT':'d','DE':'d','FL':'r','GA':'r','HI':'d','ID':'r','IL':'r','IN':'r','IO':'r','KA':'r','KY':'d','LA':'r','ME':'r','MD':'r','MA':'r','MI':'r','MN':'d','MS':'r','MO':'d','MT':'d','NE':'r','NH':'d','NJ':'r','NM':'r','NY':'d','NC':'r','ND':'r','OH':'r','OK':'r','OR':'d','PA':'d','RI':'d','SC':'r','SD':'r','TE':'r','TX':'r','UT':'r','VT':'d','VA':'d','WA':'d','WV':'d','WI':'r','WY':'r'}
svg = open('blankmap.svg', 'r').read()
soup = BeautifulSoup(svg, "lxml")
paths = soup.find_all('path')
for p in paths:
stateid = (p['id'])
if stateid in states:
print([stateid].values())
You can see my (failed) attempt at accomplishing this in the last line.
Upvotes: 2
Views: 49
Reputation: 474141
Don't you just mean:
if stateid in states:
print(states[stateid])
Upvotes: 4