在没有linebreak的情况下在python中读取文件

use*_*260 1 python

我希望能够读取文件并逐行添加一个字符串,而不会在中间插入换行符.

text_file = open("read_it.txt", "r")
print text_file.readline() + "0123456789"
Run Code Online (Sandbox Code Playgroud)

输出到

>>
text
0123456789
>>
Run Code Online (Sandbox Code Playgroud)

我希望它输出到这种格式

>>
text0123456789
>>
Run Code Online (Sandbox Code Playgroud)

How*_*ard 6

使用rstrip方法:

text_file.readline().rstrip() + "0123456789"
Run Code Online (Sandbox Code Playgroud)

  • 哦,是的,你是对的.对于那个很抱歉.好吧,你已经拥有我的+1(但仍然,'\n`就足够了,即使在Windows上也是如此). (2认同)
  • text.rstrip(os.linesep)怎么样? (2认同)