简体   繁体   中英

Going down columns using xlrd

Let's say I have a cell (9,3). I want to get the values from (9,3) to (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). How do I write a loop for that in 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.

For more information, you can read this pdf file (Page 9 Introspecting a sheet).

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.

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 . When I usually do data munging, I use R and it saves me so much time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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