def citypop():
import csv
F = open("Top5000Population.txt")
csvF = csv.reader(F)
D = {}
with csvF for row in csvF:
city,state,population = row[0],row[1],row[2]
population = population.replace(',','')
population = int(population)
city = city.upper()[:12]
D[(city, state)] = population
return D
The function citypop() returns a dict with (city,state) as the key and the population of that city (in that state) as the value.
I keep getting a syntax error .. am I not understanding the csv module correctly?
EDIT: thanks for the help guys….this should work but now all of the sudden I am getting the error
for city, state, population in reader(F): File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors[0]) UnicodeDecodeError: 'ascii' codec can't decode byte 0xa4 in position 7062: ordinal not in range(128)
When I run the test cases …. any suggestions?
Think you mean this when tried to use with statement – in such a case file will be closed right after leaving code under it:
OR: