将输出数据从`for循环`复制到Python中的文本文档

时间:2016-09-21 22:52:50

标签: file python-3.x copy document

我正在处理的代码打开一个文本文件,将文本文件打印到python终端然后遍历整个文档,复制所有带有字符'e'的单词并将其打印到终端。然后应该将这些单词写入单独的文本文档。

file = open("myfile.txt", 'r')
output = open("outputfile.txt", 'w')
file = file.read()
print(file)
enter = input('Press enter to see all words with e.')
for words in file.split(' '):
    if 'e' in words:
        print(words)
        output.write(words)

output.close()

程序运行正常,直到它必须将文本从循环复制到outputfile.txt

结果在输出文件中应如下所示:

The
extra
three.
document.
...

但是输出文件看起来像:

Thisextrathree.The
...

它保留了一些没有出现在循环中的单词并将所有内容压在一起。我不确定是什么问题。

0 个答案:

没有答案