使用 for 循环去除白色 space 并在读取文件之前重置指针
Using for loop to strip white space and resetting the pointer prior to reading a file
我正在使用 Pycharm 并且到目前为止一直很开心。但是,今天我 运行 遇到了一个我无法弄清楚或无法解释的问题。该代码将提示用户输入文件。该文件是一个包含多行文字的 .txt 文件。用户提供文件名后,程序将打开它,删除行尾的空格并打印文件的内容。 (lots_of_words.txt = 示例)
输入
print(lots_of_words.txt)
输出
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)
输出
Process finished with exit code 0
现在只需删除带有 string.rstrip() 的 for 循环,文本文件就可以正常打印:
输入
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! ....
我正在使用 python 3.4 和 Pycharm IDE。我意识到脚本没有错误地完成了,但为什么它不打印 final 变量呢?我确定这是一个简单的答案,但我无法弄清楚。
运行 Python 2.7 中的相同代码,即使使用 string.rstrip() 也能正常打印。
与PyCharm无关。
您的 for
将指针移至文件末尾。要再次使用 open_file
,请在打印前使用 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'
不会。
不相关:您应该使用 with open() as.. :
而不是 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))
我正在使用 Pycharm 并且到目前为止一直很开心。但是,今天我 运行 遇到了一个我无法弄清楚或无法解释的问题。该代码将提示用户输入文件。该文件是一个包含多行文字的 .txt 文件。用户提供文件名后,程序将打开它,删除行尾的空格并打印文件的内容。 (lots_of_words.txt = 示例)
输入
print(lots_of_words.txt)
输出
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)
输出
Process finished with exit code 0
现在只需删除带有 string.rstrip() 的 for 循环,文本文件就可以正常打印:
输入
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! ....
我正在使用 python 3.4 和 Pycharm IDE。我意识到脚本没有错误地完成了,但为什么它不打印 final 变量呢?我确定这是一个简单的答案,但我无法弄清楚。
运行 Python 2.7 中的相同代码,即使使用 string.rstrip() 也能正常打印。
与PyCharm无关。
您的 for
将指针移至文件末尾。要再次使用 open_file
,请在打印前使用 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'
不会。
不相关:您应该使用 with open() as.. :
而不是 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))