Python 如何更新历史记录并将结果写入另一个文本文件?
我需要对以下代码执行三件事:Python 如何更新历史记录并将结果写入另一个文本文件?,python,Python,我需要对以下代码执行三件事: 我需要更新历史记录\u ends=5000 我需要把所有的打印语句写入一个文本文件 我需要这样做直到文件中的行结束 history_begins = 1; history_ends = 5000; n = 0; total = 0 historyjobs = []; targetjobs = [] listsub = []; listrun = []; listavg = [];listfinal = [] def check(inputfile): f
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()
现在我需要输出以显示500150025003的结果。。。。。。。。。。。。。。直到最后&输出(print语句)将被写入另一个文本文件。代码中的哪些更改将为我提供结果。请允许任何人提出python中的解决方案实际上不太清楚您想做什么(特别是列表中的第1和第3项) 要重定向所有打印的输出,可以执行以下操作:
import sys
f = open('log.txt', 'w')
sys.stdout = f
print "test"
之后,您可以使用以下命令将标准输出设置回原始状态:sys.stdout=sys.\uuu stdout\uuu
import sys
f = open('log.txt', 'w')
sys.stdout = f
print "test"