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

read csv as dict but keep duplicate keys python

Hi currently trying to read in a very easy csv file that looks like this:
enter image description here

i don’t need the header, but i want all rows as a k,v in a dictionary but the writer overwrites the data with the last value, how do I keep these duplicates in order to keep all rows as k,v in my dict?

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

>Solution :

The convenient way to do it is using a defaultdict from the collections module:

# create some test data
with open("data.txt","w") as d:
    d.write("test,test_one\ndata,plaza\ndata,plazo\ndata,plozo\ndoto,plaza")

then use

from collections import defaultdict

data = defaultdict(list)

with open("data.txt") as f:
    f.readline()  # skip header

    # process remainder
    for line in f:
        line = line.strip() # remove \n
        if line:
            # extract key + value by splitting
            key,value = line.split(',',2)
            # and add it
            data[key].append(value)

print(data)            

# print converted 
print(dict(data.items()))            

Output:

defaultdict(<class 'list'>, {'data': ['plaza', 'plazo', 'plozo'],
                             'doto': ['plaza']})

# converted to normal dict
{'data': ['plaza', 'plazo', 'plozo'], 'doto': ['plaza']}

See How does collections.defaultdict work?

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