Excel电子表格读取特定的列python

我知道这可能是相当简单的一些,购买我是新来的python。

我一直在寻找一种专门为Excel电子表格中的列编写程序的方法。 一个例子(取自以前的问题)

Name Date Age Sex Color Ray May 25.1 M Gray Alex Apr 22.3 F Green Ann Jun 15.7 F Blue 

当我input亚历克斯时,我需要在他的行中显示Alex的所有信息。 我使用的电子表格中有数以千计的名字,第一列中的所有内容都是不同的。 我已经将xlrd和电子表格导入python(2.7)。

请帮忙!

我的代码,我有问题执行。

 from collections import namedtuple Entry = namedtuple('Entry', 'FQDN Primary Secondary') import xlrd file_location = "/Users/abriman26/Desktop/Book1.xlsx" ss_dict = {} spreadsheet = file_location = "/Users/abriman26/Desktop/Book1.xlsx" for row in spreadsheet: entry = Entry(*tuple(row)) ss_dict[entry.Name] = entry 

和错误消息

 Traceback (most recent call last): File "<pyshell#114>", line 2, in <module> entry = Entry(tuple(row)) TypeError: __new__() takes exactly 6 arguments (2 given) 

我假设你的问题是针对如何在内存中存储电子表格中的信息,以便快速查找数据,而不是如何使用xlrd库。 如果情况并非如此,请优化您的问题,并提供您所尝试的代码示例。

如果列A是键,则创build一个包含元组或名称的字典。

 from collections import namedtuple Entry = namedtuple('Entry', 'Name Date Age Sex Color') #open the spreadsheet with xlrd or other spreadsheet library and save into variable named "spreadsheet" #... ss_dict = {} for row in spreadsheet: entry = Entry(*tuple(row)) ss_dict[entry.Name] = entry >> ss_dict['Alex'] Entry(Ann, Jun, 15.7, F, Blue) 

如果您知道该人的姓名,这将使您快速随机访问电子表格中的条目,特别是如果您有多个条目。