我正在尝试使用 Python 读取文件。这是文件 test.txt
我正在使用:
Hello World
My name is Will
What's your name?
这是蟒蛇代码:
fhand = open('test.txt')
for line in fhand:
line.rstrip()
print line
无论我是否使用 line.rstrip()
,输出总是如下:
Hello World
My name is Will
What's your name?
我怎样才能像这样使用 rstrip()
在没有空行的情况下输出?
Hello World
My name is Will
What's your name?
原文由 Shiyu Lian 发布,翻译遵循 CC BY-SA 4.0 许可协议
line.rstrip()
不更改旧变量,它返回旧变量的剥离值,您必须重新分配它才能使更改生效,例如:否则该行不会更改,它会使用旧行而不是剥离的行。