且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从文件中读取行而不会得到"\ n"附加在末尾

更新时间:2023-02-22 08:07:04

要在结尾处仅删除换行符:

To remove just the newline at the end:

line = line.rstrip('\n')

readline保留换行符的原因是,您可以区分空白行(具有换行符)和文件末尾(空字符串).

The reason readline keeps the newline character is so you can distinguish between an empty line (has the newline) and the end of the file (empty string).