Home > OS >  How to convert csv file to dictionary so that top row is keys, and columns (in list form) are values
How to convert csv file to dictionary so that top row is keys, and columns (in list form) are values

Time:10-20

(To preface, I know/believe you could use numpy.loadtxt and pandas but I want to know how to achieve the same result without them.)

I would have a csv file like this, after opening it and assigning it to file while also using splitlines():

file = 
'Top1','Top2','Top3','Top4','Top5'
'1','a','3','b','5'
'a','2','b','3','c'
'1','a','3','b','5'

How could I go about converting the file into a dictionary, with the top row being the keys, and the column underneath each key being its value, like so:

a_dict = {
'top1': ['1', 'a', '1'],
'top2': ['a', '2', 'a'],
'top3': ['3', 'b', '3'],
'top4': ['b', '3', 'b'],
'top5': ['5', 'c', '5']
}

I tried by doing this first:

a_dict = {}
for i in file[0].split(','):
    a_dict[i] = ''
print(a_dict)

which gave me:

a_dict = {"'top1'": '', "'top2'": '', "'top3'": '', "'top4'": '', "'top5'": ''}

then, to get the columns into lists I tried:

prac_list = []
for i in file[1:]:
    i = i.split(',')
    x = 0
    prac_list.append(num[x])
    x =1
print (prac_list)

and that gave me:

prac_list = ["'1'", "'a'", "'1'"]

but I got stuck up to that point, the idea was to have the for loop go through elements below the first row, and have it so each loop iteration would take the x'th index of each row and append them together as one element in the list like ['1', 'a', '1']. eg:

x = 0
for i in file[1:]:
    get x'th index of each row
    append together as one element in prac_list
    x  = 1
print (prac_list)

then loop through the dict and change the values to the items in the list, but I believe I am messing up the slicing and the appending to the list part.

CodePudding user response:

You can do this with zip,

text = """'Top1','Top2','Top3','Top4','Top5'
'1','a','3','b','5'
'a','2','b','3','c'
'1','a','3','b','5'""" 
lines = text.split('\n') # This equals to the file_pointer.readlines()

keys = keys = list(map(lambda x:x.replace("'", '').lower(), lines[0].split(',')))
values = [list(map(lambda x:x.replace("'", ''), line.split(','))) for line in lines[1:]]
result = dict(zip(keys, values))

Output:

{'top1': ['1', 'a', '3', 'b', '5'],
 'top2': ['a', '2', 'b', '3', 'c'],
 'top3': ['1', 'a', '3', 'b', '5']}

CodePudding user response:

You can use the csv library to iterate through rows:

import csv


with open('file.csv') as f:
    reader_obj = csv.DictReader(f)
    
    data = next(reader_obj)
    for row in reader_obj:
        data = {k: (data[k] [v] if type(data[k]) is list else [data[k]] [v]) for k,v in row.items()}

print(data)
{"'Top1'": ["'1'", "'a'", "'1'"], "'Top2'": ["'a'", "'2'", "'a'"], "'Top3'": ["'3'", "'b'", "'3'"], "'Top4'": ["'b'", "'3'", "'b'"], "'Top5'": ["'5'", "'c'", "'5'"]}

CodePudding user response:

I think this code will help you :

with open('Book1.csv','r')as f:
    file=f.read().splitlines()

a_dict ={i.replace("'",""):[] for i in file[0].split(',')}

prac_list = []
for i in file[1:]:
    i = i.split(',')
    x=0
    for j in a_dict:
        a_dict[j].append(i[x].replace("'",""))
        x =1
print(a_dict)

Output

{'Top1': ['1', 'a', '1'], 'Top2': ['a', '2', 'a'], 'Top3': ['3', 'b', '3'], 'Top4': ['b', '3', 'b'], 'Top5': ['5', 'c', '5']}
  • Related