python将带有行和列标题的csv文件读入带有两个键的字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35829360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:01:32  来源:igfitidea点击:

python read csv file with row and column headers into dictionary with two keys

pythoncsvdictionary

提问by WanderingMind

I have csv file of the following format,

我有以下格式的csv文件,

,col1,col2,col3
row1,23,42,77
row2,25,39,87
row3,48,67,53
row4,14,48,66

I need to read this into a dictionary of two keys such that

我需要将其读入包含两个键的字典中

dict1['row1']['col2'] = 42
dict1['row4']['col3'] = 66

If I try to use csv.DictReaderwith default options

如果我尝试使用带有默认选项的csv.DictReader

with open(filePath, "rb" ) as theFile:
    reader = csv.DictReader(theFile, delimiter=',')
    for line in reader:
    print line

I get the following output

我得到以下输出

{'': 'row1', 'col2': '42', 'col3': '77', 'col1': '23'}
{'': 'row2', 'col2': '39', 'col3': '87', 'col1': '25'}
{'': 'row3', 'col2': '67', 'col3': '53', 'col1': '48'}
{'': 'row4', 'col2': '48', 'col3': '66', 'col1': '14'}

I'm not sure of how to process this output to create the type of dictionary that I'm interested in.

我不确定如何处理此输出以创建我感兴趣的字典类型。

For sake of completeness, it would also help if you can address how to write back the dictionary into a csv file with the above format

为了完整起见,如果您能解决如何将字典写回上述格式的 csv 文件,这也将有所帮助

回答by Tim Pietzcker

Using the CSV module:

使用 CSV 模块:

import csv
dict1 = {}

with open("test.csv", "rb") as infile:
    reader = csv.reader(infile)
    headers = next(reader)[1:]
    for row in reader:
        dict1[row[0]] = {key: int(value) for key, value in zip(headers, row[1:])}

回答by Romain

You can use pandasfor that even if it is a bit an overkill. The pro is that there is almost nothing to code to obtain the expected result.

您可以为此使用熊猫,即使它有点矫枉过正。优点是几乎没有什么代码可以得到预期的结果。

# Reading the file
df = pd.read_csv('tmp.csv', index_col=0)

# Creating the dict
d = df.transpose().to_dict(orient='series')

print(d['row1']['col2'])
42

回答by alecxe

The format of the input file is not exactly convenient to parse with csvmodule. I'd parse headers separately, then parse the rest line by line, splitting by ,, stripping and making dictionaries along the way. The working code:

输入文件的格式不太方便用csv模块解析。我会单独解析标题,然后逐行解析其余部分,,一路拆分,剥离和制作字典。工作代码:

from pprint import pprint

d = {}
with open("myfile.csv") as f:
    headers = [header.strip() for header in next(f).split(",")[1:]]

    for line in f:
        values = [value.strip() for value in line.split(",")]
        d[values[0]] = dict(zip(headers, values[1:]))

pprint(d)

Prints:

印刷:

{'row1': {'col1': '23', 'col2': '42', 'col3': '77'},
 'row2': {'col1': '25', 'col2': '39', 'col3': '87'},
 'row3': {'col1': '48', 'col2': '67', 'col3': '53'},
 'row4': {'col1': '14', 'col2': '48', 'col3': '66'}}