打开csv文件在python中自定义字典 [英] open csv file in python to customize dictionary
问题描述
我想知道加载此csv文件:
表位,ID,频率,测定
AVNIVGYSNAQGVDY,123431,27.0,Tetramer
DIKYTWNVPKI,887473,50.0,3H
LRQMRTVTPIRMQGG,34234,11.9,Elispot
>
d = {'AVNIVGYSNAQGVDY':[ID [123431],Frequency [27.0],Assay ['Tetramer']],'DIKYTWNVPKI':[ID [887473],Frequency [50.0],Assay ['3H']] LRQMRTVTPIRMQGG':[ID [34234],Frequency [11.9],Assay ['Elispot']]
我使用列表,因为我的实际文件是更大的,我会追加更多的值到这些列表。
解决方案首先,你知道
csv
模块?如果没有,阅读介绍和例子到你知道如何获得一个可迭代的行。打印每一个,他们将看起来像这样:['AVNIVGYSNAQGVDY','123431','27 .0' ,'Tetramer']
或者,如果使用
DictReader
:{'Epitope':'AVNIVGYSNAQGVDY','ID':'123431',
'Frequency':'27 .0','Assay':'Tetramer'}
想要列表的列表或字典的列表;你想要一个列表的字典,其中每个键是
Epitope
从一行,对应的值是其他三个值,对不对?所以...只需将每行插入字典:d = {}
with open(path, rb')as f:
with csv.DictReader(f)as reader:
for row in reader:
d [row ['Epitope']] = [row ['ID' row ['Frequency'],row ['Assay']]
一个普通的
读取器
而不是一个DictReader
,但我认为它更可读这种方式,如果你回到代码a几个月后)。
您还需要转换这些值,并将它们用作另一个查找的键,但这应该是微不足道的。例如:
row_id = ID [int(row ['ID'])]
row_frequency = Frequency [float (row ['Frequency'])]
row_assay = Assay [row ['Assay']]
d [row ['Epitope']] = [row_id,row_frequency,row_assay]
您可以通过将行转换写为函数,然后使用字典理解来清理:
以open(path,'rb')as f:
with csv.DictReader(f)as reader:
d = {row ['Epitope']:process_row(row)for reader in reader}
I would like to know to load this csv file:
Epitope,ID,Frequency,Assay AVNIVGYSNAQGVDY,123431,27.0,Tetramer DIKYTWNVPKI,887473,50.0,3H LRQMRTVTPIRMQGG,34234,11.9,Elispot
into a python dictionary like this:
d = {'AVNIVGYSNAQGVDY': [ID[123431],Frequency[27.0],Assay['Tetramer']], 'DIKYTWNVPKI': [ID[887473],Frequency[50.0],Assay['3H']], 'LRQMRTVTPIRMQGG': [ID[34234],Frequency[11.9],Assay['Elispot']]}
I am working with lists since my actual file is bigger and I will append more values to those lists.
解决方案First, do you know about the
csv
module? If not, read the introduction and examples to the point where you know how to get an iterable of rows. Print each one out, and they'll look something like this:['AVNIVGYSNAQGVDY', '123431', '27.0', 'Tetramer']
Or, if you use a
DictReader
:{'Epitope': 'AVNIVGYSNAQGVDY', 'ID': '123431', 'Frequency': '27.0', 'Assay': 'Tetramer'}
But you don't want a list of lists, or a list of dictionaries; you want a dictionary of lists, where each key is the
Epitope
from a row, and the corresponding value is the other three values, right? So… just insert each row into a dictionary that way:d = {} with open(path, 'rb') as f: with csv.DictReader(f) as reader: for row in reader: d[row['Epitope']] = [row['ID'], row['Frequency'], row['Assay']]
(This would be more compact with a plain
reader
instead of aDictReader
, but I think it's more readable this way if you come back to the code a few months from now.)You also want to convert the values and use them as keys for another lookup, but that should be trivial. For example:
row_id = ID[int(row['ID'])] row_frequency = Frequency[float(row['Frequency'])] row_assay = Assay[row['Assay']] d[row['Epitope']] = [row_id, row_frequency, row_assay]
You can clean this up a bit by writing the row transformation as a function, and then just using a dictionary comprehension:
with open(path, 'rb') as f: with csv.DictReader(f) as reader: d = {row['Epitope']: process_row(row) for row in reader}
这篇关于打开csv文件在python中自定义字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!