简体   繁体   English

如何使用Gpread或XLWT从子列表的列表中创建表格数据结构?

[英]How to create a tabular data structure from a list of sublists with Gpread or XLWT?

How do you create a tabular data structure form this list of sublists using GSpread or XLWT in which every item at index 0 is in the first column, every item at index 1 is in the second column, etc.? 如何使用GSpread或XLWT从此子列表列表创建表格数据结构,其中索引0的每个项目在第一列中,索引1的每个项目在第二列中,依此类推?

For example, I'd like to put all the a's in the following list into column1, all the b's into column2, etc. In other words, I only want one value to one cell, so I'd like 'a' in the first cell in the first column, 'aa1' in the second cell of the first column, 'aa2' in the third cell of the first column, etc. 例如,我想将以下列表中的所有a放入column1,将所有b放入column2,依此类推。换句话说,我只想要一个值到一个单元格,所以我想在第一列的第一个单元格,第一列的第二个单元格中的“ aa1”,第一列的第三个单元格中的“ aa2”,依此类推。

 lst = [[['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']],[['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']]]

This is what I have, which is using a for-loop, but I'm wondering if there is a different method where I could create one for loop that way I wouldn't have to manually create a for loop for every extra column. 这就是我使用的for循环,但是我想知道是否存在其他方法可以创建一个for循环,而不必为每个额外的列手动创建for循环。

    gc = gspread.login('username', 'password')
    sheet = gc.open("Curalate").sheet1

    row = 1
    for subsublist in lst[0]:
         sheet.update_cell(1,row,subsublist[0])
         row = row + 1
    row = 1 
    for subsublist in lst[0]:
         sheet.update_cell(2,row,subsublist[1])
         row = row + 1
             row = 1 
    for subsublist in lst[0]:
         sheet.update_cell(,row,subsublist[2])
         row = row + 13

also, if this were xlwt, it's exactly the same except sheet.udpate_cell would be replaced with sheet.write and it's organized sheet.write(row,column,datapoint) instead of (column, row, datapoint). 另外,如果这是xlwt,则完全相同,只是sheet.udpate_cell将被sheet.write替换,并且它是有组织的sheet.write(row,column,datapoint)而不是(column,row,datapoint)。

Based on the comment: "each sublist with have its own sheet", I think you can do this quite simply with a nested loop. 基于注释:“每个子列表都有自己的工作表”,我认为您可以使用嵌套循环非常简单地完成此操作。 I'm familiar with xlwt but not gspread, so I'll demonstrate with xlwt syntax: 我熟悉xlwt但不熟悉gspread,因此我将以xlwt语法进行演示:

# assume worksheet is an xlwt worksheet object
lst = [['a','b','c'],['aa1','bb1','cc1'],['aaa2','bbb2','ccc2']]
for (rownum, rowlist) in enumerate(lst):
    for (colnum, value) in enumerate(rowlist):
        worksheet.write(rownum, colnum, value)

Both enumerate and xlwt are 0-indexed (at least in the Python interface) so you don't need to change that. enumeratexlwt都是0索引的(至少在Python接口中),因此您无需更改它。 If gspread is 1-indexed (as the Excel human interface is), adding 1 to rownum and/or colnum as needed should correct that. 如果gspread是1索引的(就像Excel人机界面一样),则根据需要在rownum和/或colnum中添加1应该可以解决该问题。

It sounds like you'll have more than one worksheet, depending on the number of two-dimensional sublists in your main list. 听起来您将有多个工作表,具体取决于主列表中二维子列表的数量。 Again, you can nest for that: 同样,您可以为此嵌套:

wb = xlwt.Workbook()
for (sheet_number, sublist) in enumerate(lst):
    ws = wb.add_sheet('sheet_%s', sheet_number + 1)
    for (rownum, rowlist) in enumerate(sublist):
        for (colnum, value) in enumerate(rowlist):
            ws.write(rownum, colnum, value)

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

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