首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Python初学者)输出文件在我的代码中为空

(Python初学者)输出文件在我的代码中为空
EN

Stack Overflow用户
提问于 2017-10-09 10:54:37
回答 3查看 208关注 0票数 2

我是Python的新手,在文件中处理输入和输出。以下是输入文件:

代码语言:javascript
复制
1 3
1 1
1 0
20 30

下面是我的代码,它将其视为"soccer_in.txt“,并假设将以下内容输出到”soccer_out.txt“中:

代码语言:javascript
复制
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

使用以下代码:

代码语言:javascript
复制
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)

我没有得到任何错误,但我的输出文件是空的,当我运行我的代码。我不确定发生了什么,任何帮助都将不胜感激。谢谢!

编辑:到目前为止,提出的解决方案都没有奏效。我运行了该文件,但仍然是空白的“file output.txt”。我看到了关闭文件的问题,但这并没有解决输出文件为空的事实。

编辑2:不要紧!我在我的计算机上打开了输入文件,这不允许代码工作。谢谢大家

EN

回答 3

Stack Overflow用户

发布于 2017-10-09 11:05:25

您应该将您的代码重写到上下文管理器表单中:

代码语言:javascript
复制
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)

有了上下文管理器,文件对象将自动关闭。所以你不需要担心结束语。

票数 0
EN

Stack Overflow用户

发布于 2017-10-09 11:06:44

问题是您在打开文件后无法将其关闭。为了防止出现这种情况,请使用with上下文管理器来读写文件。

代码语言:javascript
复制
with open(file_name, 'r') as f:
    f.read()
with open(file_name, 'w') as f:
    f.write(data)
票数 0
EN

Stack Overflow用户

发布于 2017-10-09 11:19:21

您不需要关闭文件。尽管你应该这么做。

垃圾收集器会处理它的。但是,您确实需要更改代码,使用\n处理换行符,并且还需要在process_seasons函数中将行拆分为单词。..The下面的代码在我的计算机上运行,并提供您正在寻找的输出。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46638201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档