而循环没有进行到下一个迭代

2024-10-01 17:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是下面代码中的try语句。我从每月数据文件中提取范围内的数据,并将其输出到一个整合文件,以便Power BI读取

可能出现的一个问题是,如果工程师打开了合并文件,我将在尝试写入此文件时遇到权限错误。我的解决办法是尝试5次。如果5次都失败了,忽略写入文件,他们需要等到下一次运行此脚本以获取更新的数据,这是对他们保持打开状态的惩罚

#=================================================================================
# Create the consolidated file that power BI will use
#=================================================================================
def createDashboardFile(self):
    outputData = pd.DataFrame(columns=consolidatedHeaders)
    rangeStart = datetime.datetime.today() + datetime.timedelta(days=-self.dashboardRange)
    months = (datetime.datetime.today().month - rangeStart.month) + 1
    for month in range(0,months):
        path=self.consolidatedPath + r'\\' + str(rangeStart.year) + str(('00' + str(rangeStart.month))[-2:]) + '.csv'
        if os.path.exists(path):
            inputData = pd.read_csv(path)
            inputData.columns = inputData.columns.str.strip()
            inputData['Timestamp']= pd.to_datetime(inputData['Timestamp'])
            inScope = inputData['Timestamp'] >= rangeStart
            #Remove rows where the date is not in range
            inputData = inputData[inScope]
            outputData = outputData.append(inputData, ignore_index=True)
            rangeStart = rangeStart.replace(day=1)
            rangeStart = rangeStart + datetime.timedelta(days=32)
            rangeStart = rangeStart.replace(day=1)
    tryCount = 0
    while tryCount < 5:
        try:
            with open(self.biFile, 'w') as o:
                outputData.to_csv(o, header=True, index=False)
            tryCount = 5
        except:
            tryCount = tryCount =+ 1
            if tryCount < 5:
                logEvent('BI File Write Error #'+ str(tryCount) + ', ' + os.path.basename(self.biFile), False)
            else:
                logEvent('BI File Write Failed, ' + os.path.basename(self.biFile), False)

问题是代码在tryCount = 1处循环,并将无限循环,直到成功写入。我在try语句中做错了什么?或者,为了获得额外的积分,有没有更好的方法来处理试图写入某人未打开的文件

BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv
BI File Write Error #1, Makino.csv

Tags: 文件csvpathselfdatetimeerrorfilewrite
3条回答

试着改变

        tryCount = 0
        while tryCount < 5:
            try:
                with open(self.biFile, 'w') as o:
                    outputData.to_csv(o, header=True, index=False)
                tryCount = 5
            except:
                tryCount = tryCount =+ 1
                if tryCount < 5:
                    logEvent('BI File Write Error #'+ str(tryCount) + ', ' + os.path.basename(self.biFile), False)
                else:
                    logEvent('BI File Write Failed, ' + os.path.basename(self.biFile), False)

        for _ in range(5):
            try:
                with open(self.biFile, 'w') as o:
                    outputData.to_csv(o, header=True, index=False)
                break
            except:
                tryCount = tryCount =+ 1
                if tryCount < 5:
                    logEvent('BI File Write Error #'+ str(tryCount) + ', ' + os.path.basename(self.biFile), False)
                else:
                    logEvent('BI File Write Failed, ' + os.path.basename(self.biFile), False)
            

以下行有一个错误:

tryCount = tryCount =+ 1

应该是:

tryCount += 1

tryCount = tryCount =+ 1实际上是将tryCount变量设置为1

您需要执行以下操作:tryCount += 1

相关问题 更多 >

    热门问题