简体   繁体   English

使用xlrd向下浏览列

[英]Going down columns using xlrd

Let's say I have a cell (9,3). 假设我有一个单元格(9,3)。 I want to get the values from (9,3) to (9,99). 我想要从(9,3)到(9,99)的值。 How do I go down the columns to get the values. 我如何在列中向下获取值。 I am trying to write the values into another excel file that starts from (13, 3) and ends at (13,99). 我正在尝试将值写入到另一个从(13,3)开始到(13,99)结尾的Excel文件中。 How do I write a loop for that in xlrd? 如何在xlrd中为此编写循环?

def write_into_cols_rows(r, c):
    for num in range (0,96):
        c += 1
    return (r,c)

worksheet.row(int) will return you the row, and to get the value of certain columns, you need to run row[int].value to get the value. worksheet.row(int)将返回该行,并且要获取某些列的值,您需要运行row[int].value来获取该值。

For more information, you can read this pdf file (Page 9 Introspecting a sheet). 有关更多信息,您可以阅读 pdf文件(第9页,自省一张纸)。

import xlrd
workbook = xlrd.open_workbook(filename)
# This will get you the very first sheet in the workbook.
worksheet = workbook.sheet_by_name(workbook.sheet_names()[0])
for index in range(worksheet.nrows):
    try:
        row = worksheet.row(index)
        row_value = [col.value for col in row]
        # now row_value is a list contains all the column values
        print row_value[3:99]
    except:
        pass

To write data to Excel file, you might want to check out xlwt package. 要将数据写入Excel文件,您可能需要签出xlwt软件包。

BTW, seems like you are doing something like reading from excel.. do some work... write to excel... I would also recommend you take a look at numpy , scipy or R . 顺便说一句,似乎您正在执行一些操作,例如从excel阅读....做一些工作...写给excel ...我也建议您看看numpyscipyR When I usually do data munging, I use R and it saves me so much time. 当我通常进行数据处理时,我使用R并节省了很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM