Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

python - How to read a csv into a dict where the values are a list - then use this for different for different csv files

I am trying to read a csv file into a dictionary where for each row, the first column of the csv file will be a dictionary key and the remaining columns will be the values in a list (ignoring the header row).

I have got a solution that works but I have hardcoded the columns and what I want is something that works on any number of columns (because I have multiple csv files with different numbers of columns that I want to use it on).

My data in one of the csv files is:

name,test1,test2,test3
dave,66,74,62
rob,59,65,60
nic,71,68,73

This is the code that I have used

import sys
import csv

# Create a dict (where the values are a list) to store the data in memory
database = {}

# Open the csv file and read the contents into memory
filename = sys.argv[1]
with open(filename, "r") as file:
    reader = csv.reader(file)
    # Ignore the header
    next(reader)
    for row in reader:
        # Read the first column of the csv (name) as the key, then read the remaining columns as a list for the values
        database[row[0]] = [int(row[1]), int(row[2]), int(row[3])]
    print(database)

The output for the print(database) is

{'dave': [66, 74, 62], 'rob': [59, 65, 60], 'nic': [71, 68, 73]}

The problem I have is that when I need to read in another csv file with more columns, this code will only work for 4 columns (unless I extend it) for row[4], row[5] etc...

Is there a way I can rewrite the code to just read in row[1] all the way through to row[n] in the csv file? Where n is the number of the last column in the csv file.

Any help is greatly appreciated. Just started learning Python, I have been searching but I wasn't able to find anything to help. Thanks in advance.

question from:https://stackoverflow.com/questions/65876960/how-to-read-a-csv-into-a-dict-where-the-values-are-a-list-then-use-this-for-di

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you considered pandas?

filename = sys.argv[1]
df = pd.read_csv(filename)
dct = dict(zip(df['name'], df.drop('name', axis=1).apply(list, axis=1)))

With the data you show as example:

>>> dct
{'dave': [66, 74, 62], 'rob': [59, 65, 60], 'nic': [71, 68, 73]}

If instead you want to use csv and loops, you can do it too (slower would be my guess):

with open(filename) as file:
    reader = csv.reader(file)
    # Ignore the header
    next(reader)
    database = {row[0]: [int(x) for x in row[1:]] for row in reader}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...