简体   繁体   中英

Writing to a certain row/column using XLWT

My data is in the form of a list of lists and I would like to begin writing the data in row 1, column zero. I am using the xlwt library.

def to_spreadsheets(data_one):
    workbook = xlwt.Workbook()
    sheet_one = workbook.add_sheet("July 2013 Stats")


    data_one_trimmed = data_one['rows']

    for rownum, sublist in enumerate(data_one_trimmed):
        for colnum, value in enumerate(sublist):
            sheet_one.write(rownum, colnum, value)

This code writes my data to row 0, column 0 as I would like it to, but I have no idea how to change the location of where it writes to.

I tried sheet_one.write(1, 0, rownum, colnum, value) , but it obviously did not work.

Any thoughts?

If you wish to "offset" a row/column by some value, you can use a second argument to enumerate which is where it starts counting. enumerate will start from 0 by default, but you can start it from 1 (or any integer value) using its second argument, eg: enumerate(some_iterable, 1) .

The docs are https://docs.python.org/2/library/functions.html#enumerate which states:

enumerate(sequence, start=0)

Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:

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