python中\n和\r\n的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28228383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:57:50  来源:igfitidea点击:

Difference between \n and \r\n in python

pythonutf-8

提问by David542

I have a script that sometimes prints a newline as \nand sometimes uses \r\n. What is the difference between the two, and is one preferable over the other?

我有一个脚本,有时会打印换行符\n,有时会使用\r\n. 两者之间有什么区别,一个比另一个更可取吗?

采纳答案by Cui Heng

"\n" is the class Unix/linux style for new line. "\r\n" is the default Windows style for line separator. "\r" is classic Mac style for line separator. I think "\n" is better, because this also looks good on windows, but some "\r\n" may not looks so good in some editor under linux, such as eclipse or notepad++. If you are handling some protocols, the "\r\n" is usually required.

"\n" 是用于换行的类 Unix/linux 样式。"\r\n" 是行分隔符的默认 Windows 样式。"\r" 是经典的 Mac 风格的行分隔符。我认为“\n”更好,因为这在windows上看起来也不错,但是有些“\r\n”在linux下的某些编辑器中可能看起来不太好,例如eclipse或notepad++。如果您正在处理某些协议,通常需要“\r\n”。

回答by dmitri

Code

代码

print "\n",
print "\r",
print "\r\n",

gives you the following output in hex

为您提供以下十六进制输出

0a 0d 0d 0a

See also answers hereand here.

另请参阅此处此处的答案。