Python 3.x 选择数字列值通过openpyxl更改符号的行
我正在学习Python和openpyxl,以便在大型xlsx工作簿上进行数据分析。我有一个Python 3.x 选择数字列值通过openpyxl更改符号的行,python-3.x,openpyxl,Python 3.x,Openpyxl,我正在学习Python和openpyxl,以便在大型xlsx工作簿上进行数据分析。我有一个for循环,可以遍历整个列。以下是一些示例数据: ROW: VALUE: 1 1 2 2 3 3 4 4 5 -4 6 -1 7 -6 8 2 9 3 10 -3 我想打印出值从正值变为负值的行,反之亦然。因此,在上面的示例中,第5、8和10行将在控制台中打印。如何在for循环中使用if语句迭代openpyxl上的
for
循环,可以遍历整个列。以下是一些示例数据:
ROW: VALUE:
1 1
2 2
3 3
4 4
5 -4
6 -1
7 -6
8 2
9 3
10 -3
我想打印出值从正值变为负值的行,反之亦然。因此,在上面的示例中,第5、8和10行将在控制台中打印。如何在for
循环中使用if
语句迭代openpyxl上的列
到目前为止,我可以打印列中的所有单元格:
import openpyxl
wb = openpyxl.load_workbook('ngt_log.xlsx')
sheet = wb.get_sheet_by_name('sheet1')
for i in range(1, 10508, 1): # 10508 is the length of the column
print(i, sheet.cell(row=i, column=6).value)
我的想法是在for循环的中添加一个if
语句:
for i in range(1, 10508, 1): # 10508 is the length of the column
if(( i > 0 and (i+1) < 0) or (i < 0 and (i+1) > 0)):
print((i+1), sheet.cell(row=i, column=6).value)
对于范围(1,10508,1)中的i:#10508是列的长度
如果((i>0和(i+1)<0)或(i<0和(i+1)>0)):
打印((i+1),表格单元格(行=i,列=6).value)
但这不起作用。如果
陈述正确,我是否制定了陈述?在我看来,你的陈述似乎自相矛盾
for i in range(1, 10508, 1): # 10508 is the length of the column
if(( i greater than 0 and (i+1) less than 0) or (i less than 0 and (i+1) greater than
0)):
print((i+1), sheet.cell(row=i, column=6).value)
我用通俗易懂的英语写了>和<符号,但如果I大于0,则I+1永远不小于0,反之亦然,因此它们永远不会起作用,因为两者不可能都是真的在我看来,你的说法似乎自相矛盾
for i in range(1, 10508, 1): # 10508 is the length of the column
if(( i greater than 0 and (i+1) less than 0) or (i less than 0 and (i+1) greater than
0)):
print((i+1), sheet.cell(row=i, column=6).value)
我用简单的英语编写了>和<符号,但如果I大于0,则I+1永远不小于0,反之亦然,因此它们将永远不起作用,因为两者都不能为真您需要先获取sheet.cell值,然后进行比较:
end_范围=10508
对于范围内的i(1,结束范围):
当前,next=sheet.cell(行=i,列=6).值,sheet.cell(行=i+1,列=6).值
如果当前>0且下一个<0或当前<0且下一个>0:
打印(i+1,下一个)
我很确定数学库中有一个sign()函数,但有点过分了。如果值为0,您可能还需要确定要执行的操作。您需要先获取sheet.cell值,然后进行比较:
end_范围=10508
对于范围内的i(1,结束范围):
当前,next=sheet.cell(行=i,列=6).值,sheet.cell(行=i+1,列=6).值
如果当前>0且下一个<0或当前<0且下一个>0:
打印(i+1,下一个)
我很确定数学库中有一个sign()函数,但有点过分了。您可能还想知道如果值为0,您想做什么。您可以编写规则来选择符号已更改的行,并将它们放入生成器表达式中,而不需要使用额外内存,如下所示:
pos = lambda x: x>=0
keep = lambda s, c, i, v: pos(s[c][x].value)!=pos(v.value)
gen = (x+1 for x, y in enumerate(sheet['f']) if x>0 and keep(sheet, 'f', x-1, y))
然后,当您需要知道符号已更改的行时,只需在gen
上迭代,如下所示:
for row in gen:
# here you use row
您可以编写规则来选择符号已更改的行,并将其放入生成器表达式中,而无需使用额外内存,如下所示:
pos = lambda x: x>=0
keep = lambda s, c, i, v: pos(s[c][x].value)!=pos(v.value)
gen = (x+1 for x, y in enumerate(sheet['f']) if x>0 and keep(sheet, 'f', x-1, y))
然后,当您需要知道符号已更改的行时,只需在gen
上迭代,如下所示:
for row in gen:
# here you use row
您可以使用标志来检查正负
ws = wb['sheet1'] # why people persist in using long deprecated syntax is beyond me
flag = None
for row in ws.iter_rows(max_row=10508, min_col=6, max_col=6):
cell = row[0]
sign = cell.value > 0 and "positive" or "negative"
if flag is not None and sign != flag:
print(cell.row)
flag = sign
您可以使用标志来检查正负
ws = wb['sheet1'] # why people persist in using long deprecated syntax is beyond me
flag = None
for row in ws.iter_rows(max_row=10508, min_col=6, max_col=6):
cell = row[0]
sign = cell.value > 0 and "positive" or "negative"
if flag is not None and sign != flag:
print(cell.row)
flag = sign