简体   繁体   中英

Converting the contents of a CSV file into a dictionary

The code I have so far is in a function that basically reads a csv file and prints it's 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', '']

required output:

{'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 that output? Using Python 3.4.2 if that helps, explanation of your answer will be appreciated!

using the csv standard library and a dictionary comprehension...

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 want. d[1:-1] slices the array from the second to the second to last element.

EDIT: skips headers, converts to tuples

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'))   

Prints:

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

Not to much to explain here. Just putting the values in the dictionary, and skipping the first row added. I assumed that the persons names are unique.

So... I know this question has mostly been answered, but I thought I'd just throw a one-liner in the mix to add on to the shortening 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 novel thing is adding islice to skip the header row cleanly.

Use DictReader:

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

Here is my solution if I may:

>>> 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 on pe.load and to_dict of pyexcel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM