简体   繁体   English

无法让python写入文本文件,给我一个空文件

[英]Can't get python to write to a text file, gives me an empty file

I am trying to get my program to write a list to a text file. 我正在尝试让我的程序将列表写入文本文件。 I've changed it to a string and it creates the text file but it is empty. 我将其更改为字符串,并创建了文本文件,但它为空。 Also I got it to print the variable I'm trying to write to the text file and it comes out exactly as it is supposed to. 我也得到它来打印要尝试写入文本文件的变量,它的输出完全符合预期。

import string
import re
wholeLine = ""
file = open("bowlingscores.txt", "r")
for line in file:
    line = line.strip()
    wholeLine += line
    scores = re.findall('\d+', wholeLine)
    names = re.findall('\D+', wholeLine)
file.close()
scores = list(map(int,scores))
validScores = [x for x in scores if 300 >= x >= 0]
average = sum(validScores) / len(validScores)
numScores = len(scores)
output = []
for i in range(numScores):
    if scores[i] == 300:
        output.append(names[i])
        output.append("\nperfect")
    if scores[i] == average:
        output.append(names[i])
        output.append("\naverage")
    if scores[i] < average:
        output.append(names[i])
        output.append("\nbelow average")
    if scores[i] > average:
        if scores[i] <300:
            output.append(names[i])
            output.append("\nabove average")
    if scores[i] > 300:
        output.append(names[i])
        output.append("\ninvalid score")
outputFile = open('bowlingaverages.txt', 'w')
outputFile.write(str(output))
outputFile.close
print(output)

you should convert your output which is a list to string properly, try using 您应该将列表的输出正确转换为字符串,请尝试使用

outputFile = open('bowlingaverages.txt', 'w')
outputFile.write(''.join(output))
outputFile.close()

Here is how you'd write to the file with output (which is a list): 这是通过输出(列表)写入文件的方式:

with open('bowlingaverages.txt', 'w') as f:
    f.write(''.join(output))
print(output)

Update: I edited the code as follows and had a friend run it on his computer and it did as it was supposed to, however still does not work on my computer. 更新:我按如下方式编辑了代码,并让一个朋友在他的计算机上运行它,并且按预期的方式运行,但是仍然无法在我的计算机上运行。 I am now more confused than I was in the first place. 现在,我比起初时更加困惑。 Is it possibly a problem with a different program? 其他程序可能有问题吗?

import string
import re
wholeLine = ""
file = open("bowlingscores.txt", "r")
for line in file:
    line = line.strip()
    wholeLine += line
    scores = re.findall('\d+', wholeLine)
    names = re.findall('\D+', wholeLine)
file.close()
scores = list(map(int,scores))
validScores = [x for x in scores if 300 >= x >= 0]
average = sum(validScores) / len(validScores)
numScores = len(scores)
output = []
for i in range(numScores):
    if scores[i] == 300:
        output.append(names[i])
        output.append("\tperfect\n")
    if scores[i] == average:
        output.append(names[i])
        output.append("\taverage\n")
    if scores[i] < average:
        output.append(names[i])
        output.append("\tbelow average\n")
    if scores[i] > average:
        if scores[i] <300:
            output.append(names[i])
            output.append("\tabove average\n")
    if scores[i] > 300:
        output.append(names[i])
        output.append("\tinvalid score\n")
outputFile = open('bowlingaverages.txt', 'w')
for item in output:
    outputFile.write(item)
outputFile.close
print(output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM