使用 XLRD 循环访问列中的行



我已经能够让列在单独的列表中输出列的值。但是,我需要保留这些值并逐个使用它们来执行亚马逊查找。亚马逊查找不是问题。让XLRD一次给出一个值一直是一个问题。在 Python 中是否也有设置时间的有效方法?我发现计时器问题的唯一答案是记录进程开始的时间并从那里计数。 我更喜欢一个计时器。这个问题有点像两部分,这是我到目前为止所做的。

我使用 argv[

1] 加载带有 xlrd 的电子表格,我使用 argv[2] 将其复制到新的电子表格名称; argv[3] 我需要成为计时器实体,但我还没有那么远。

我试过:

import sys
import datetime
import os
import xlrd
from xlrd.book import colname
from xlrd.book import row
import xlwt
import xlutils
import shutil
import bottlenose
AMAZON_ACCESS_KEY_ID = "######"
AMAZON_SECRET_KEY = "####"
print "Executing ISBN Amazon Lookup Script -- Please be sure to execute it python amazon.py input.xls output.xls 60(seconds between database queries)"
print "Copying original XLS spreadsheet to new spreadsheet file specified as the second arguement on the command line."
print "Loading Amazon Account information . . "
amazon = bottlenose.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY)
response = amazon.ItemLookup(ItemId="row", ResponseGroup="Offer Summaries", SearchIndex="Books", IdType="ISBN")
shutil.copy2(sys.argv[1], sys.argv[2])
print "Opening copied spreadsheet and beginning ISBN extraction. . ."
wb = xlrd.open_workbook(sys.argv[2])
print "Beginning Amazon lookup for the first ISBN number."
for row in colname(colx=2):
    print amazon.ItemLookup(ItemId="row", ResponseGroup="Offer Summaries", SearchIndex="Books", IdType="ISBN")

我知道这有点模糊。如果我尝试做类似 column = colname(colx=2) 的事情,那么我可以对列中的行做:任何帮助或指导都非常感谢。

在代码中使用 colname() 只是返回列的名称(例如,在您的情况下默认为"C",除非您已覆盖该名称)。 此外,colname 的使用超出了工作簿内容的上下文。 我认为您会想要使用正在加载的工作簿中的特定工作表,并且从该工作表中您希望引用列的值(在您的示例中为 2),这听起来有点正确吗?

wb = xlrd.open_workbook(sys.argv[2])
sheet = wb.sheet_by_index(0)
for row in sheet.col(2):
        print amazon.ItemLookup(ItemId="row", ResponseGroup="Offer Summaries", SearchIndex="Books", IdType="ISBN")

虽然我认为查看对amazon.ItemLookup()的调用,您可能希望引用row而不是"row",因为后者只是一个字符串,而前者是来自 for 循环的名为 row 的变量的实际内容。

最新更新