Python 换行符常量如 C++标准::endl

发布于 2024-12-19 07:05:35 字数 282 浏览 2 评论 0原文

我正在格式化字符串(在魔术方法 __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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铃予 2024-12-26 07:05:35

使用 os.linesep 怎么样?它包含适合您的操作系统的行分隔符:

>>> import os
>>> os.linesep
'\n'
>>> print "line one" + os.linesep + "line two"
line one
line two

How about using os.linesep? It contains the appropriate line separator for your OS:

>>> import os
>>> os.linesep
'\n'
>>> print "line one" + os.linesep + "line two"
line one
line two
浅浅 2024-12-26 07:05:35

不。使用 \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.

一身软味 2024-12-26 07:05:35

使用 \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 the file-object takes care of that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文