Python 换行符常量如 C++标准::endl
我正在格式化字符串(在魔术方法 __str__
中),并且我不想使用原始 "\n"
或 "\r\n"< /代码>。
python 是否有跨平台换行符,例如 std::endl
在 C++ 中?
我尝试用谷歌搜索,但找不到答案。
I am formatting a string (in magic method __str__
) and I don't want to use a raw "\n"
or "\r\n"
.
Does python have a cross-platform newline identifier like std::endl
in C++?
I tried to Google it, but couldn't find an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 os.linesep 怎么样?它包含适合您的操作系统的行分隔符:
How about using
os.linesep
? It contains the appropriate line separator for your OS:不。使用
\n
。在 C++ 中,std::endl
只不过是"\n" << std::flush
,并且它不更具跨平台性,无论这意味着什么。它只是速度较慢,因为它强制冲洗。No. Use
\n
. And in C++,std::endl
is nothing other than"\n" << std::flush
, and it is not more cross-platform, whatever that would mean. It's only slower, as it forces flushing.使用
\n
没有任何问题 - 为什么不想使用它?跨平台问题仅在写入文件时才相关,并且file
对象会为您解决这个问题。There is nothing wrong with using
\n
- why don't you want to use it? The cross-platform problem is only relevant when writing to files, and thefile
-object takes care of that for you.