Excel电子表格读取特定列python

Excel电子表格读取特定列python,python,excel,xlrd,Python,Excel,Xlrd,我知道这对一些人来说可能相当简单,但我是python新手 我一直在寻找一种方法,专门为Excel电子表格上的一个列编写一个程序。 示例(取自上一个问题) 我需要在我键入Alex时显示Alex所在行中的所有信息。我使用的电子表格有数千个名称,其中第一列中的所有内容都不同。我已经将xlrd和电子表格导入python(2.7) 请帮忙! 我的代码在执行时遇到问题 以及错误消息 Traceback (most recent call last): File "<pyshell#114>",

我知道这对一些人来说可能相当简单,但我是python新手

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

我需要在我键入Alex时显示Alex所在行中的所有信息。我使用的电子表格有数千个名称,其中第一列中的所有内容都不同。我已经将xlrd和电子表格导入python(2.7)

请帮忙! 我的代码在执行时遇到问题

以及错误消息

Traceback (most recent call last):
File "<pyshell#114>", line 2, in <module>
entry = Entry(tuple(row))
TypeError: __new__() takes exactly 6 arguments (2 given)
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
条目=条目(元组(行))
TypeError:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我假设您的问题更多的是针对如何将电子表格中的信息存储在内存中,以便快速查找数据,而不是如何使用xlrd库。如果不是这样,请改进您的问题,并包含您尝试过的代码示例

如果列A是键,则创建包含元组或命名元组的字典

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)

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

我感谢您的帮助!实际上,我花了这一周和上一周的时间学习Python,你完全正确,这就是我所需要的。我运行了你的代码,但一直得到错误回溯(最近一次调用上次):文件“”,第2行,在entry=entry(*tuple(row))TypeError:\uuuu new\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!在此处了解解包参数列表运算符(*):和。要自行解决此问题,请尝试在for循环中打印
len(tuple(row))
。长度是否与所有行的
条目
namedtuple类的定义一致?它工作正常,谢谢!不幸的是,现在他们希望我将其转换为csv,然后使用
csv
python模块(
import csv
)执行完全相同的操作嘿,已经几天了,我仍然无法完全获取它,我的csv有1000多行,3列,我需要读取特定的行,但不是基于数字的。我看到的唯一教程显示了第[5]行,我需要输入实际名称aj56exp.tub.com,它旁边是两个名称。我是否应该用一个干净的开始再发布一个问题?
Traceback (most recent call last):
File "<pyshell#114>", line 2, in <module>
entry = Entry(tuple(row))
TypeError: __new__() takes exactly 6 arguments (2 given)
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)