打开csv文件在python中自定义字典

编程入门 行业动态 更新时间:2024-10-27 08:33:50
本文介绍了打开csv文件在python中自定义字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道加载此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 a DictReader, 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中自定义字典

本文发布于:2023-11-05 21:24:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字典   文件   csv   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!