Python 3.x 使用for循环在读取文件之前去除空白并重置指针
我正在使用Pycharm,到目前为止我一直很开心。然而,今天我遇到了一个我无法理解或解释的问题。代码将提示用户输入文件。该文件是一个.txt文件,其中包含几行字。用户提供文件名后,程序将打开文件名,删除行尾的空白,并打印文件内容。(lots_of_words.txt=示例) 输入Python 3.x 使用for循环在读取文件之前去除空白并重置指针,python-3.x,pycharm,Python 3.x,Pycharm,我正在使用Pycharm,到目前为止我一直很开心。然而,今天我遇到了一个我无法理解或解释的问题。代码将提示用户输入文件。该文件是一个.txt文件,其中包含几行字。用户提供文件名后,程序将打开文件名,删除行尾的空白,并打印文件内容。(lots_of_words.txt=示例) 输入 print(lots_of_words.txt) Programming is fun and will save the world from errors! .... Process finished wi
print(lots_of_words.txt)
Programming is fun and will save the world from errors! ....
Process finished with exit code 0
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
Programming is fun and will save the world from errors! ....
输出
print(lots_of_words.txt)
Programming is fun and will save the world from errors! ....
Process finished with exit code 0
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
Programming is fun and will save the world from errors! ....
以下是导致混淆的代码部分:
user_input = input('Enter the file name: ')
open_file = open(user_input)
for line in open_file:
line = line.rstrip()
read_file = open_file.read()
print(read_file)
输出
print(lots_of_words.txt)
Programming is fun and will save the world from errors! ....
Process finished with exit code 0
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
Programming is fun and will save the world from errors! ....
现在,只需使用string.rstrip()删除for循环,文本文件就可以很好地打印:
输入
print(lots_of_words.txt)
Programming is fun and will save the world from errors! ....
Process finished with exit code 0
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
Programming is fun and will save the world from errors! ....
输出
print(lots_of_words.txt)
Programming is fun and will save the world from errors! ....
Process finished with exit code 0
user_input = input('Enter the file name: ')
open_file = open(user_input)
# Removed for loop
read_file = open_file.read()
print(read_file)
Programming is fun and will save the world from errors! ....
我正在使用Python3.4和Pycharm IDE。我意识到脚本完成得很好,没有错误,但是为什么它不打印最终的变量呢?我相信这是一个简单的答案,但我想不出来
在Python2.7中运行相同的代码,即使使用string.rstrip()也可以很好地打印 这与PyCharm无关 您的
for
将指针移动到文件的末尾。要再次使用打开\u文件
,请在打印前使用seek(0)
open_file = open(user_input)
for line in open_file:
line = line.rstrip()
open_file.seek(0)
read_file = open_file.read()
print(read_file)
但这并不是最有效的解决方案(如果在给定的情况下效率很重要的话),因为所有的行都读了两遍。您可以在阅读后存储每一行(如另一个答案中所建议的),也可以在条带化后打印每一行
另外,rstrip()
将删除字符串末尾的空白,但不会删除“\n”
无关:您应该将open()作为..使用
:
而不是open()
,因为它会自动关闭文件 在for循环中迭代文件对象将消耗它,因此将没有任何内容可供读取,您只需丢弃所有行
如果要删除所有行中的所有空白,可以使用:
user_input = input('Enter the file name: ')
open_file = open(user_input)
lines = []
for line in open_file:
lines.append(line.rstrip())
print(''.join(lines))
甚至更短:
print(''.join(line.rstrip() for line in open_file))