Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

Its just the way you are printing it. When you print(my_dict), you get the single dictionary (AKA, hash table) you constructed. If you want to display in a different format, you can do it yourself. In your case, you seem to want to make each key/value pair in the dictionary look like its own dict. So, build and print.

for state, cities in my_dict.items():
    print({state: cities})

There are different ways to print. For instance

for state, cities in my_dict.items():
    print(f"{state}: {', '.join(cities)}")

Would print

Alabama: Alexander City, Decatur, Florence, ...

There are many options for display.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading