gpt4 book ai didi

python - 合并后的xls输出不同

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:25 25 4
gpt4 key购买 nike

我希望脚本的输出如下:

2   11
4 12
6 13
8 14
48 15

但是输出是这样的:

2   48
4 48
6 48
8 48
48 48

下面是代码,有人可以帮我解决我做错的事情吗:

import xlwt
import xlrd
import sys ; sys.path
#Create workbook and worksheet

wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Data_1')#name of the sheet in which new data is there

row = 0

f = xlrd.open_workbook('Data_1.xls')#workbook from where data is extracted
g = xlrd.open_workbook('Data_2.xls')#workbook from where data is extracted

sheet1 = f.sheet_by_index(0)

sheet2 = g.sheet_by_index(0)

for i in xrange(5):
temp = sheet1.row_values(i)
print temp
col = 0
for element in temp:
sheet.write(i,col,element)
col += 1
i += 1

for j in xrange(5):
temp1 = sheet2.row_values(j)
print temp1
col = 1
for elements in temp1:
sheet.write(j,col,element)
col +=1
j += 1

wbk.save('DATA.xls') #excel sheet name when saved in the folder

最佳答案

在第二个循环中,变量名称有拼写错误。

for elements in temp1:

应该是

for element in temp1:
<小时/>

编辑:您的示例中还有很多冗余代码。我冒昧地重写了您的程序,使其更加简洁(尽管请注意,我还没有测试过这一点):

import xlwt
import xlrd
import sys
#Create workbook and worksheet

wbk = xlwt.Workbook()
dest_sheet = wbk.add_sheet('Data_1')

for sheet_idx in range(1, 3):
sheet = xlrd.open_workbook('Data_%d.xls' % sheet_idx).sheet_by_index(0)

for i in range(5):
values = sheet.row_values(i)
for idx, element in enumerate(values):
dest_sheet.write(i, sheet_idx+idx, element)

wbk.save('DATA.xls')

关于python - 合并后的xls输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19414209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com