2
試圖寫一個腳本,將(最終)美國地圖顏色紅色或藍色取決於州州長是共和黨還是民主黨。在嘗試實際着色地圖之前,我需要從dict'states'中得到給定狀態的'r'或'd'值。基本上,我無法匹配從stateid = (p['id'])
返回的文本字符串與字典states
中同名鍵的值。任何人都可以幫我弄清楚如何做到這一點?遇到麻煩匹配字典鍵值
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())
您可以在最後一行看到我的(失敗)嘗試完成此操作。
...哇。我現在覺得很愚蠢。感謝@alecxe! – n1c9