2013-08-19 54 views
0

我需要做三件事情,下面的代碼:如何更新歷史並將結果寫入其他文本文件?

  1. 我需要更新history_ends = 5000

  2. 我需要編寫所有的print語句到一個文本文件

  3. 我需要這樣做,直到文件中的行結束爲止

    history_begins = 1; history_ends = 5000; n = 0; total = 0 
    historyjobs = []; targetjobs = [] 
    listsub = []; listrun = []; listavg = [];listfinal = [] 
    
    def check(inputfile): 
        f = open(inputfile,'r') 
        lines = f.readlines() 
        for line in lines: 
        job = line.split() 
        if(int(job[0]) < history_ends): 
         historyjobs.append(job) 
        else: 
         targetjobs.append(job) 
        print len(historyjobs) 
        print len(targetjobs) 
        print targetjobs[0] 
        j = 0   
        for i in range(len(historyjobs)): 
        if((int(historyjobs[i][3]) == int(targetjobs[j][3])) and (int(historyjobs[i][4]) == int(targetjobs[j][4])) and (int(historyjobs[i][5]) == int(targetjobs[j][5]))): #i am comparing 3rd,4th & 5th columns of list historyjobs and targetjobs 
    
         listsub.append(historyjobs[i][1]) #storing the column num 1 to listsub 
    
         listrun.append(historyjobs[i][2]) #storing the column num 1 to listrun 
    
        print listsub 
        print len(listsub)     
        print listrun 
    
    def runningMean(seq, n=0, total=0): 
        if not seq: 
        return [] 
        total =total+int(seq[-1]) 
        return runningMean(seq[:-1], n=n+1, total=total) + [total/float(n+1)] 
    
    def main(): 
    check('newfileinput') 
    listavg = runningMean(listsub,n = 0,total = 0) 
    print listavg 
    for i in range(len(listsub)): 
        if (int(listsub[i]) > float(listavg[i] * 0.9)): 
         listfinal.append(listsub[i]) 
    print listfinal 
    
    if __name__ == '__main__': 
         main() 
    

代碼的輸出是:

 3756 
    215077 
    ['5000', '1710390', '930', '8', '9', '2'] 
    ['767220', '769287', '770167', '770276', '770791', '770835', '771926', '1196500',  '1199789', '1201485', '1206331', '1206467', '1210929', '1213184', '1213204', '1213221', '1361867', '1361921', '1361949', '1364886', '1367224', '1368005', '1368456', '1368982', '1369000', '1370365', '1370434', '1370551', '1371492', '1471407', '1709408', '1710264', '1710308', '1710322', '1710350', '1710365', '1710375'] 
    37 
    ['2717', '184', '188', '163', '476', '715', '1099', '716', '586', '222', '456', '457', '582', '418', '424', '425', '177', '458', '236', '2501', '3625', '1526', '299', '1615', '1632', '1002', '379', '3626', '1003', '1004', '3625', '1002', '1019', '1037', '1066', '998', '977'] 
    [1282960.6216216215, 1297286.75, 1312372.4571428571, 1328319.6764705882, 1345230.0909090908, 1363181.3125, 1382289.2580645161, 1402634.7, 1409742.7931034483, 1417241.142857143, 1425232.111111111, 1433651.3846153845, 1442738.76, 1452397.5, 1462798.0869565217, 1474143.2727272727, 1486568.142857143, 1492803.2, 1499691.7368421052, 1507344.111111111, 1515724.0, 1525005.25, 1535471.9333333333, 1547401.642857143, 1561126.2307692308, 1577136.75, 1595934.1818181819, 1618484.2, 1646032.3333333333, 1680349.875, 1710198.857142857, 1710330.6666666667, 1710344.0, 1710353.0, 1710363.3333333333, 1710370.0, 1710375.0] 
    ['1361867', '1361921', '1361949', '1364886', '1367224', '1709408', '1710264', '1710308', '1710322', '1710350', '1710365', '1710375'] 

現在我需要的輸出中顯示的結果爲5001,5002,5003 ..............直到結束&的輸出(打印語句)寫在另一個文本文件中。代碼中的哪些變化將爲我提供結果。Plz可以提供任何一種python解決方案

回答

1

實際上並不清楚你想做什麼編號爲1和3的列表)

要重定向所有打印輸出,您可以這樣做:

import sys 
f = open('log.txt', 'w') 
sys.stdout = f 
print "test" 

之後,您可以設置返回設置標準輸出到原來的狀態:sys.stdout = sys.__stdout__

相關問題