2016-05-17 83 views
-4

我有一些代码在当前终端打印信息?我怎样才能把它写到一个txt文件中呢?如何将每个打印行写入一个txt文件

def find_bets(): 
for bet in bets: 
    team_name1 = bet[1] 
    team_name2 = bet[2] 
    league = bet[9] 
    lines = [bet[4], bet[5]] 
    period = bet[7] 
    handicap = bet[6] 
    print "-------------------------------"  
    print league 
    print game_hour, hours_to_start 
    print team_name1, team_name2 
    print lines 
    print handicap 
    print t1, (game_hour - hours1)/24. 
    print t2, (game_hour - hours2)/24. 
    base_prob = t1 * t2 

    print "BASE PROB", base_prob 
+4

请做一些之前的研究提出了一个问题。 – Jerrybibo

回答

0

如果你想写既输出和文件中,你可以做

f = open("filename", "w") 

def print_both(str): 
    print str 
    f.write(str+"\n") 

以及拨打替换find_bets每次调用printprint_both