python - End of line in CSV -
i trying read csv file processing in python. want associate values of cities state in dictionary present in csv comma separated. after getting name of state want cities values want know end of line of file.
my structure of csv :
state,city1,city2,city3,city4,city5,..,cityn
since states may not have values cities, city values empty.
if iterate on file, automatically yield each line; in other words don't need know line ends:
from collections import defaultdict d = defaultdict(list) open('somefile.csv') f: line in f: # automatically step on each line correctly bits = line.split(',') d[bits[0]] += bits[1:] state,cities in d.items(): print('{} has {} cities: '.format(state, len(cities)) city in cities: print('\t{}'.format(city))
Comments
Post a Comment