0

我需要对以下代码做三件事:

  1. 我需要更新 history_ends = 5000

  2. 我需要将所有打印语句写入文本文件

  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 的结果............直到结束&输出(打印语句)要写入另一个文本文件。有什么变化代码将为我提供结果。任何人都可以在 python 中提出解决方案

4

1 回答 1

1

实际上,您想做什么并不清楚(特别是您列表中的第 1 和第 3 项)

要重定向所有打印的输出,您可以执行以下操作:

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

之后,您可以使用以下命令将 set stdout 设置回原始状态:sys.stdout = sys.__stdout__

于 2013-08-19T07:00:46.973 回答