[英](Python Beginner) Output file is empty in my code
我是Python的新手,致力於文件的輸入和輸出。 這是輸入文件:
1 3
1 1
1 0
20 30
這是我的代碼,將其作為“ soccer_in.txt”,並假設將以下內容輸出到“ soccer_out.txt”:
Season: 1, Games Played: 1, Points earned: 3
Possible Win-Tie-Loss Records
-----------------------------
1-0-0
Season: 2, Games Played: 1, Points earned: 1
Possible Win-Tie-Loss Records
-----------------------------
0-1-0
Season: 3, Games Played: 1, Points earned: 0
Possible Win-Tie-Loss Records
-----------------------------
0-0-1
Season: 4, Games Played: 20, Points earned: 30
Possible Win-Tie-Loss Records
-----------------------------
10-0-10
9-3-8
8-6-6
7-9-4
6-12-2
5-15-0
使用此代碼:
def process_season(output_file, season, games_played, points_earned):
output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
", Points earned: " + str(points_earned))
output_file.write("Possible Win-Tie-Loss Records")
output_file.write("-----------------------------")
wins = int(points_earned) // 3
ties = int(points_earned) % 3
losses = int(games_played) - wins - ties
while (wins >= 0) and (losses >= 0):
output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses))
wins -= 1
ties += 3
losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
season_number = 0
for season in input_file:
season_number += 1
process_season(output_file, season_number, season[0], season[1])
# --------------------------------------
f_in=open("soccer-in.txt", "r")
f_out=open("soccer-out.txt", "w+")
process_seasons(f_in, f_out)
我沒有收到任何錯誤,但是運行代碼時輸出文件為空。 我不確定發生了什么,任何幫助將不勝感激。 謝謝!
編輯:到目前為止,所有提議的解決方案都沒有起作用。 我運行該文件,“ soccer-output.txt”仍然為空。 我看到了關閉文件的問題,但是並沒有解決輸出文件為空的事實。
編輯2:沒關系! 我在計算機上打開了輸入文件,這不允許代碼工作。 謝謝你們
您應該將代碼重寫為上下文管理器形式:
def process_season(output_file, season, games_played, points_earned):
output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
", Points earned: " + str(points_earned))
output_file.write("Possible Win-Tie-Loss Records")
output_file.write("-----------------------------")
wins = int(points_earned) // 3
ties = int(points_earned) % 3
losses = int(games_played) - wins - ties
while (wins >= 0) and (losses >= 0):
output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses))
wins -= 1
ties += 3
losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
season_number = 0
for season in input_file:
season_number += 1
process_season(output_file, season_number, season[0], season[1])
# --------------------------------------
with open("soccer-in.txt", "r") as f_in:
with open("soccer-out.txt", "w+") as f_out:
process_seasons(f_in, f_out)
使用上下文管理器,文件對象將自動關閉。 因此,您不必擔心結賬。
問題是打開文件后您無法關閉文件。 為了幫助防止這種情況,請使用with上下文管理器來讀取和寫入文件。
with open(file_name, 'r') as f:
f.read()
with open(file_name, 'w') as f:
f.write(data)
您不需要關閉文件。 雖然你應該。
垃圾收集器會照顧好它。 但是您確實需要更改代碼,使用\\n
來考慮換行符,並且還需要在process_seasons
函數中將行拆分為單詞。 ..以下代碼在我的計算機上運行,並提供您要查找的輸出。
def process_season(output_file, season, games_played, points_earned):
output_file.write("Season: " + str(season) + ", Games Played: " + str(games_played) +
", Points earned: " + str(points_earned) + '\n')
output_file.write("Possible Win-Tie-Loss Records\n")
output_file.write("-----------------------------\n")
wins = int(points_earned) // 3
ties = int(points_earned) % 3
losses = int(games_played) - wins - ties
while (wins >= 0) and (losses >= 0):
output_file.write(str(wins) + "-" + str(ties) + "-" + str(losses)+'\n')
wins -= 1
ties += 3
losses -= 2
# --------------------------------------
def process_seasons(input_file, output_file):
season_number = 0
for season in input_file:
season_number += 1
seas = season.split()
process_season(output_file, season_number, seas[0], seas[1])
# --------------------------------------
f_in=open("soccer-in.txt", "r")
f_out=open("soccer-out.txt", "w+")
process_seasons(f_in, f_out)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.