Convert CSV file contents to dictionary

The code that I still have in the function that basically reads the csv file and prints its contents:

def read(filename): with open(filename, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: print(row) 

Contents of sailor.csv :

 name, mean performance , std dev Alice, 100, 0, Bob, 100, 5, Clare, 100, 10, Dennis, 90, 0, Eva, 90, 5, 

read('sailor.csv') and running the function

current output:

 ['name', ' mean performance ', ' std dev'] ['Alice', ' 100', ' 0', ''] ['Bob', ' 100', ' 5', ''] ['Clare', ' 100', ' 10', ''] ['Dennis', ' 90', ' 0', ''] ['Eva', ' 90', ' 5', ''] 

output required:

 {'Dennis': (90.0, 0.0), 'Clare':(100.0, 10.0), 'Eva': (90.0, 5.0), 'Bob': (100.0, 5.0), 'Alice': (100.0, 0.0)} 

Any ideas how I can achieve this conclusion? Using Python 3.4.2, if that helps, explain your answer!

+6
source share
5 answers

I think this is what you want:

 import csv def read(filename): out_dict = {} with open(filename, 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') next(csvfile) # skip the first row for row in reader: out_dict[row[0]] = float(row[1]), float(row[2]) print(row) return out_dict print(read('data.csv')) 

Print

 {'Bob': (' 100', ' 5'), 'Clare': (' 100', ' 10'), 'Alice': (' 100', ' 0'), 'Dennis': (' 90', ' 0'), 'Eva': (' 90', ' 5')} 

Not worth explaining here. Just put the values ​​in the dictionary and skip the added first line. I suggested that people's names are unique.

+2
source

using csv standard library and dictionary understanding ...

 import csv with open('sailor.csv') as csvfile: reader = csv.reader(csvfile) next(reader) d = {r[0] : tuple(r[1:-1]) for r in reader} 

Where d will be the dictionary you need. d[1:-1] truncates the array from the second to the second to the last element.

EDIT: skips headers, converts to tuples

+8
source

So ... I know that this question was mainly answered, but I thought that I would just drop a single-line layer in the mix to add to the reduction of answers:

 from csv import reader from itertools import islice {r[0] : tuple(r[1:-1]) for r in islice(reader(open('sailor.csv')), 1, None)} 

The only really new thing is to add islice to skip the title bar cleanly.

+2
source

Use DictReader:

 def read(filename): with open(filename, 'r') as csvfile: reader = csv.DictReader(csvfile, delimiter=',') for row in reader: print(row) 
0
source

Here is my solution if I can:

 >>> import pyexcel as pe >>> s = pe.load("sailor.csv", name_rows_by_column=0, name_columns_by_row=0) >>> s.format(float) >>> s Sheet Name: csv +--------+------------------+---------+---+ | | mean performance | std dev | | +========+==================+=========+===+ | Alice | 100 | 0 | 0 | +--------+------------------+---------+---+ | Bob | 100 | 5 | 0 | +--------+------------------+---------+---+ | Clare | 100 | 10 | 0 | +--------+------------------+---------+---+ | Dennis | 90 | 0 | 0 | +--------+------------------+---------+---+ | Eva | 90 | 5 | 0 | +--------+------------------+---------+---+ >>> del s.column[''] # delete the column which has '' as its name >>> s.to_dict(True) # make a dictionary using row names as key OrderedDict([('Alice', [100.0, 0.0]), ('Bob', [100.0, 5.0]), ('Clare', [100.0, 10.0]), ('Dennis', [90.0, 0.0]), ('Eva', [90.0, 5.0])]) 

Here is the documentation for pe.load and to_dict pyexcel

0
source

Source: https://habr.com/ru/post/979377/


All Articles