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
该函数返回一个以该城市(在该州)的人口为键和作为citypop()
值的字典。(city,state)
我不断收到语法错误..我没有正确理解 csv 模块吗?
编辑:谢谢你们的帮助....这应该可以工作,但现在突然间我得到了错误
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)
当我运行测试用例时....有什么建议吗?