简体   繁体   English

编写数据以迭代地表现出色

[英]Writing data to excel iteratively

I have 2 lists and below is what i wanna do: b = ['ibm cloud','dell cloud'] a =['wen','see','there'] 我有2个列表,下面是我想做的事情:b = ['ibm cloud','dell cloud'] a = ['wen','see','there']

I want to create 2 different worksheets with the list items in b. 我想用b中的列表项创建2个不同的工作表。 and then write to the worksheets the list items from a in the rows. 然后将行中的列表项写入工作表。 And i am unable to do so. 我无法这样做。

import xlwt base = xlwt.Workbook() for a in a: sheet = base.add_sheet(a) for b in b: sheet.write(row,col,b)

Please help 请帮忙

You said create 2 different worksheets with items in b, but you use items in a in your code. 您说过用b中的项目创建2个不同的工作表,但是在代码中使用a中的项目。 Why? 为什么? And try not to use same name when using for loop. 并在使用for循环时尽量不要使用相同的名称。 With this code, you can get a xls with two sheets, named 'company1' and 'company2'. 使用此代码,您可以获得带有两个工作表的xls,分别名为“ company1”和“ company2”。 In each sheet, from row 0 to row 2, there will be value defined in a. 在每个工作表中,从第0行到第2行,将在a中定义值。 Last but not least, don't forget to save. 最后但并非最不重要的一点是,不要忘记保存。 Try this: 尝试这个:

#!/usr/bin/python
#-*- coding:utf-8 -*-

import xlwt

base = xlwt.Workbook()

b = ['company1','company2']
a = ['a', 'b', 'c']

for name in b:
    n = 0
    s = base.add_sheet(name);
    for v in a:
        s.write(n, 0, v)
        n += 1

base.save('C:\\test.xls')

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

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