Create a python dictionary from a .csv file

I have a comma separated file listing the fifty states followed by cities in that state. The structure of the file is as follows:

Alabama, Alexander City, Decatur, Florence, Gadsden, Greenville, Guntersville, Huntsville,...
Arizona, Ajo, Avondale, Bisbee, Casa Grande, Chandler, Clifton,...
Arkansas, Arkadelphia, Arkansas, West Memphis,...
California, Alameda, Alhambra, Anaheim, Yorba Linda, Yuba City,...
...

The first item of each row is the state followed by the list of cities. The number of cities varies from state to state. I want to create a dictionary such that the state is the key who's value is the list of cities.

Here is my code:

import csv

# initialize an empty dictionary, and an empty list
my_dict = dict()
cities = list()

# read the csv file
with open('cities.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    # process each row
    for row in csvreader:
        cities = row
        # pop the first item (state) off the list
        new_key = cities.pop(0)
        # store the key:value entry into the dictionary
        my_dict[new_key] = cities

# Print the results
print(my_dict)

I expected this: Results similar to a hash table.

{'Alabama' : ['Alexander City', 'Decatur', 'Florence', 'Gadsden', 'Greenville', 'Guntersville', 'Huntsville',]}
{'Arizona' : ['Ajo', 'Avondale', 'Bisbee', 'Casa Grande', 'Chandler', 'Clifton',]}
{'Arkansas' : ['Arkadelphia', 'Arkansas', 'West Memphis',]}
{'California' : ['Alameda', 'Alhambra', 'Anaheim', 'Yorba Linda', 'Yuba City',]}

but got this:

{'Alabama': [' Alexander City', ' Decatur', ' Florence', ' Gadsden', ' Greenville', ' Guntersville', ' Huntsville', ''], 'Arizona': [' Ajo', ' Avondale', ' Bisbee', ' Casa Grande', ' Chandler', ' Clifton', ''], 'Arkansas': [' Arkadelphia', ' Arkansas', ' West Memphis', ''], 'California': [' Alameda:', ' Alhambra', ' Anaheim', ' Yorba Linda', ' Yuba City', '']}

As you can see, I got one long row with the row having all the states and their associated cities. Maybe it is just the way I am printing it? Considering the actual csv file has up to 20 or more cities, some with 40 or more, for each state - the output is ridiculously long.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation