Translating State Names in Python -


i have list of cities , states in csv: row[0] cities, row[1] full state names. want row[2] (currently empty) state abbreviations.

i have list this:

name_to_abbr: {"vermont": "vt", "georgia": "ga", "iowa": "ia", ... } 

how use this? eg (pseudocode)

if row[1].upper() == (one of first items in pair sets):      row[2] = (the corresponding second item in pair) 

name_to_abbr dictionary, not list. have several options accessing contents:

use try:

try:     row[2] = name_to_abbr[row[1].upper()] except keyerror:     pass 

use dict.get:

row[2] = name_to_abbr.get(row[1].upper(), "") 

or check keys in:

s = row[1].upper() if s in name_to_abbr:     row[2] = name_to_abbr[s] 

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -