How can i get values in different ranges of cells?

不羁岁月 提交于 2019-12-06 06:25:12

I believe one of the easiest solution is to use izip_longest from itertools module, this way:

>>> a=[1,2,3,4,5]
>>> b=[6,7,8,9,10]
>>> c=[2,3,4,5,6]
>>> c_ = [[x] for x in c]
>>> c_
[[2], [3], [4], [5], [6]]
>>> data = list(izip_longest(a,[],b,[],*c_, fillvalue=''))
>>> for i in data:
    l = []
    for j in i:
        l.append(j)
    print l

[1, '', 6, '', 2, 3, 4, 5, 6]
[2, '', 7, '', '', '', '', '', '']
[3, '', 8, '', '', '', '', '', '']
[4, '', 9, '', '', '', '', '', '']
[5, '', 10, '', '', '', '', '', '']

Now you can easily write it to your excel file:

for row, array in enumerate(data):
    for col, value in enumerate(array):
        sheet1.write(row, col, value)

I assumed you are using Python2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!