由于我是编程新手,因此需要您的帮助,因此我的知识仅限于出于自己的兴趣而学到的东西。 基本上,我有一个包含以下数据的Excel文件:
我要对此执行以下逻辑步骤。
X=ws.['C1']
。 Y将= X-5
&print('X=' + str(X))
检查单元格C2是否小于或等于Y;
一世。如果是,则依次为Y=Cell ['C2']
和print('Y=' +str(Y))
现在X将是下一个单元格,即X = ws。['C3']。 Y =新的X-5。
&print('X=' + str(X))
。
再次检查第2点中提到的相同条件(循环)。
ii。如果否,即C2> Y,则Y = Cell [C2] -5。 再次检查第2点中提到的情况。
我正在使用以下代码,我知道这是错误的。
import openpyxl
from openpyxl import load_workbook
import datetime
wb = load_workbook('D:/Python/data.xlsx')
ws = wb.active
X=float(ws["C2"].value)
print('X=' +str(X))
Y=float(X - 5)
for row in range(2, ws.max_row + 1):
cell=float(ws['C' +str(row)].value)
if cell < Y:
Y=cell
print('Y='+str(Y))
else:
Y=cell-5
X=float(ws['C' +str(row)+1].value)
print('X=' +str(X))
答案 0 :(得分:0)
from openpyxl import load_workbook
work_book = load_workbook("62357026/source.xlsx")
work_sheet = work_book.active
buying_price = work_sheet["C2"].value # Assuming all data are integer.
loss_threshold = buying_price - 5
print(f"Price = {buying_price}\nStarting Step 2:")
for index, row in enumerate(work_sheet.rows):
a, b, c = row # (<Cell 'Sheet1'.Ax>, <Cell 'Sheet1'.Bx>, <Cell 'Sheet1'.Cx>)
print(f'\nrow {index}: {a.coordinate} {b.coordinate} {c.coordinate}')
print(f'row {index}: {a.value} {b.value} {c.value}')
price = row[2].value
if price <= loss_threshold:
loss_threshold = price
print(f"threshold = {loss_threshold}")
else:
buying_price = price
loss_threshold = buying_price - 5
print(f"threshold = {loss_threshold}")
结果:
Price = 81
Starting Step 2:
row 0: A1 B1 C1
row 0: Mango Monday 31
threshold = 31
row 1: A2 B2 C2
row 1: Mango Tuesday 81
threshold = 76
row 2: A3 B3 C3
row 2: Mango Wednesday 89
threshold = 84
row 3: A4 B4 C4
row 3: Mango Thursday 84
threshold = 84
row 4: A5 B5 C5
row 4: Mango Friday 22
threshold = 22
row 5: A6 B6 C6
row 5: Mango Saturday 56
threshold = 51
row 6: A7 B7 C7
row 6: Mango Sunday 53
threshold = 48
row 7: A8 B8 C8
row 7: Mango Monday 94
threshold = 89
Process finished with exit code 0