Excel循环不能执行Excel Python

时间:2018-06-04 23:31:41

标签: python excel python-3.x

当我在这里测试它完全正常

str

但是当我在大型数据集中使用完全相同的循环时,它似乎永远都会继续下去。我不明白我已经让循环持续了10多分钟。

import openpyxl, pprint
from openpyxl import load_workbook
import pandas as pd
import numpy as np
import os

df = pd.DataFrame(np.random.randn(15, 12), columns=list('ABCDEFGHIJKL'))
writer = pd.ExcelWriter('test_1.xlsx', engine='openpyxl')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.close()

wb = load_workbook('test_1.xlsx')
sheet1 = wb['Sheet1']
my_list = []
for j in range(2, 8):
    my_list.append(sheet1.cell(row = 7, column = j).value)

def Standardized_C3():
        """=(AR2-MIN(AR$2:AR$17))/(MAX(AR$2:AR$17)-MIN(AR$2:AR$17))"""
        ar = 2
        while ar < sheet1.max_row:
            for rowNum in range(2, sheet1.max_row + 1):  # skip the first row
                sheet1.cell(row=rowNum, column=46).value = ('=IFERROR((AR' + str(ar) + '-MIN(AR$2:AR$' + str(sheet1.max_row) + '))/(MAX(AR$2:AR$' + str(sheet1.max_row) + 
                                                            ')-MIN(AR$2:AR$' + str(sheet1.max_row) + ')),"null")')
                ar+= 1
    Standardized_C3()

发现错误: def Standardized_C3(): """=(AR2-MIN(AR$2:AR$15100))/(MAX(AR$2:AR$15100)-MIN(AR$2:AR$15100))""" ar = 2 while ar < sheet1.max_row: for rowNum in range(2, sheet1.max_row + 1): # skip the first row sheet1.cell(row=rowNum, column=46).value = ('=IFERROR((AR' + str(ar) + '-MIN(AR$2:AR$' + str(sheet1.max_row) + '))/(MAX(AR$2:AR$' + str(sheet1.max_row) + ')-MIN(AR$2:AR$' + str(sheet1.max_row) + ')),"null")') ar+= 1 Standardized_C3() 永远需要str(sheet1.max_row)然后将x = sheet1.max_row添加到公式

1 个答案:

答案 0 :(得分:0)

尝试使用print进行调试

例如:

def Standardized_C3():
    """=(AR2-MIN(AR$2:AR$15100))/(MAX(AR$2:AR$15100)-MIN(AR$2:AR$15100))"""
    for rowNum in range(2, sheet1.max_row + 1):  # skip the first row
        print(rowNum)
        sheet1.cell(row=rowNum, column=46).value = ('=IFERROR((AR' + str(ar) + '-MIN(AR$2:AR$' + str(sheet1.max_row) + '))/(MAX(AR$2:AR$' + str(sheet1.max_row) + 
                                                    ')-MIN(AR$2:AR$' + str(sheet1.max_row) + ')),"null")')
Standardized_C3() 

并查看脚本“冻结”的位置