gpt4 book ai didi

python - 将值从一张纸复制到另一张纸时出错

转载 作者:行者123 更新时间:2023-12-01 02:23:05 25 4
gpt4 key购买 nike

我试图从某些单元格复制值,但它给了我这个错误,即使不使用 def cell(x,y) 我也尝试过,但仍然出现相同的错误。

这是错误:

learn_tar.cell(row=learn_tar, column=1).value = sheet.cell(row=learn_tar, column=1).value
AttributeError: 'int' object has no attribute 'cell'

来源:

import openpyxl 

def cell(x,y):
cell = sheet.cell(row=x,column=y).value
return cell;

def percentage(percent, whole):
return int((percent * whole) / 100.0);


ex = openpyxl.load_workbook("Final_excel2.xlsx")
sheet = ex.get_sheet_by_name('Sheet1')

num = [0,0,0]
per = [0,0,0]

for row in range(2,4798):

if cell(row,1) == '1: Progression':
num[0] = num[0] + 1
elif cell(row,1) == '2: Incidence':
num[1] = num[1] + 1
elif cell(row,1) == '3: Non-exposed control group':
num[2] = num[2] + 1

for column in range(2,49):
#doing stuff

per[0] = percentage(70,num[0])
per[1] = percentage(70,num[1])
per[2] = percentage(70,num[2])

learn_att = ex.create_sheet('Learn-Att',2)
learn_tar = ex.create_sheet('Learn-Tar',3)

test_att = ex.create_sheet('Test-Att',4)
test_tar = ex.create_sheet('Test-Tar',5)

learn_att = 1
learn_tar = 1

test_att = 1
test_tar = 1



for row in range(2,4798):

if row<=1391:
if row<=974:
learn_tar.cell(row=learn_tar, column=1).value = cell(row,1)
learn_att+= 1
learn_tar+= 1
else:
test_tar.cell(row = test_tar,column = 1).value = cell(row,1)
test_att+= 1
test_tar+= 1

for column in range(2,49):

if row<=1391:
if row<=974:
learn_att.cell(row = learn_att,column = column - 1).value = cell(row,column)
else:
test_att.cell(row = test_att,column = column - 1).value = cell(row,column)

最佳答案

您用 1 覆盖 learn_tar:

learn_tar = ex.create_sheet('Learn-Tar',3)
...
learn_tar = 1

删除:

learn_tar = 1

和:

learn_tar+= 1

来自您的代码。

关于python - 将值从一张纸复制到另一张纸时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743714/

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