2015-12-14 54 views
1

我有一個應用程序在最後創建csv文件以保存結果。我想我的應用程序,在每個應用程序run.My產生不同的CSV文件生成報告如下在每次運行時創建csv文件

def writeToCSVFile(self,csvFilePath,testResultList): 
    #Open a CSV file object 
    reportname = "toxedo_report0.csv" 
    csvFilObj=open(csvFilePath+reportname,"wb") 
    #writing CSV file with the statistical values 
    mywritter=csv.writer(csvFilObj) 
    for rowVal in testResultList: 
     mywritter.writerows(rowVal) 
    #Closing the CSV file object 
    csvFilObj.close() 

testResultList是一個類型列表。有沒有辦法避免硬編碼報告名稱?我想知道如何在每次運行中創建不同的報告。

first run - C:/report/toxedo_report0.csv 
      C:/report/toxedo_report1.csv 
      C:/report/toxedo_report2.csv 

回答

1

只需使用一個額外的參數counter

def writeToCSVFile(self,csvFilePath,testResultList, counter): 
    #Open a CSV file object 
    reportname = "toxedo_report{}.csv".format(counter) 
    csvFilObj=open(csvFilePath+reportname,"wb") 
    #writing CSV file with the statistical values 
    mywritter=csv.writer(csvFilObj) 
    for rowVal in testResultList: 
     mywritter.writerows(rowVal) 
    #Closing the CSV file object 
    csvFilObj.close() 

這是重要的一行:

reportname = "toxedo_report{}.csv".format(counter) 

{}將與數counter更換。

現在這樣調用:

首先運行:

inst.writeToCSVFile(csvFilePath, testResultList, 0) 

第二輪:

inst.writeToCSVFile(csvFilePath, testResultList, 1) 

這裏inst是有方法writeToCSVFile一個類的實例。

+0

@PythonDev這是否適合您? –

+0

Muller是的,它是。謝謝:) – PythonDev

相關問題