如何将每个打印行写入txt文件

时间:2016-05-17 03:30:14

标签: python

我有一些代码可以在终端中打印信息吗?如何将每行写入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

1 个答案:

答案 0 :(得分:0)

如果要将两者都写入输出和文件中,可以执行

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

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

通过调用print

替换find_bets中对print_both的每次通话