AnsweredAssumed Answered

Python: Replacing strings in a list with values from a dictionary.

Question asked by tktobin9 on Nov 24, 2017
Latest reply on Nov 27, 2017 by rvburton

I am writing code to edit the field names in a csv file to be imported into ArcMap. I have all the field names in a list of strings. I have a dictionary with the current field name as the key, and the new field name as the value. I am trying to iterate through the list of current field names and replace them with the values in the dictionary. I am trying to use the .replace() method and it is not working for me. Here is my current code. Any suggestions?

 

headersDict = {'Number; SEX AND AGE - Total Population':'TPop', 'Number; SEX AND AGE - Male Population':'MPop', 'Number; SEX AND AGE - Female Population':'FPop',
'Under 5 years': '<5', '5 to 9 years': '5_9', '10 to 14 years': '10_14', '15 to 19 years': '15_19', '20 to 24 years': '20_24', '25 to 29 years': '25_29',
'30 to 34 years': '30_34', '35 to 39 years': '35_39', '40 to 44 years': '40_44', '45 to 49 years': '45_49', '50 to 54 years': '50_54', '55 to 59 years': '55_59'
,'60 to 64 years': '60_64', '65 to 69 years': '65_69', '70 to 74 years': '70_74', '75 to 79 years': '75_79', '80 to 84 years': '80_84', '85 years and over': '85+',
'Median age(years)': 'Medage', '16 years and over': '16+', '18 years and over': '18+', '21 years and over': '21+', '62 years and over': '62+',
'65 years and over': '65+'}


for header in headersList:
header.replace('Number; SEX AND AGE - Total Population', headersDict['Number; SEX AND AGE - Total Population'])
outfile.write(header)

Outcomes