\r\n的区别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/transformer_WSZ/article/details/78766866

不同系统上敲”Enter”键产生的字符:

Windows 类Unix
\r\n \n


符号 ASCII码 缩写 意义
\n 10 LF(Line Feed) 换到当前位置的下一行,而不会回到行首
\r 13 CR(Carriage Return) 回到当前行的行首,而不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖



现给出如下的python代码:

print("this is the first line",end='\n')
print("this is the second line",end='\r')
print("this is the third line",end='\n')
print("this is the fourth line",end='\r')
print("this is the fifth line",end='\n')
print("end")

结果如下图所示:

结果

    在类Unix系统中,每行结尾只有 \n ;在Windows系统中,每行结尾为 \r\n 。一个直接后果是,类Unix(例如Mac、Linux)系统的文件在Windows里打开的话,所有的文字会变成一行;而Windows里的文件在类Unix系统下打开的话,\r 会被替换为 ^M 符号。

原因如下:

    类Unix中遇到 \n 会进行回车+换行的操作,回车符反而会因为无法解析而变成 ^M 显示,不发生回车的操作。而Windows中需要 \r\n 才能实现回车+换行,缺少一个控制符或者顺序不对都不能正确的另起一行。

猜你喜欢

转载自blog.csdn.net/transformer_WSZ/article/details/78766866