Quantcast
Channel: Active questions tagged filereader - Stack Overflow
Viewing all articles
Browse latest Browse all 107

Create a dictionary from a csv file with rows

$
0
0

I'm have a csv file

id,name,surname,age"1, Johny, Black, 25""2, Armando, White, 18" "3, Jack, Brown, ''""4, Ronn, Davidson, ''""5, Bill, Loney, 35"

first row this is list, other rowsHow i can be converted this csv in dictionary. With future filter and sort

import csvdicts = list()with open("test.csv", "r", encoding="utf-8") as file:    csv_reader = csv.reader(file)    field_list = list()    record_list = list()    line_counter = 0    for row in csv_reader:        if line_counter == 0:            field_list = row            line_counter += 1        else:            records = row[0].split(',')            record_list.append(records)counter = 0full = dict()for record in record_list:    for field in field_list:        try:            if field in full.keys():                full[field].append(record[counter])                counter += 1            else:                full[field] = [record[counter]]            if counter == len(record):                break        except Exception as e:            pass print(full)

My code convert only 2 rows. I'm try split rows, but this don't help me.Documentation csv lib not help me. Maybe someone knows solution


Viewing all articles
Browse latest Browse all 107

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>