前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python换行符问题:\r\n还是\n?[通俗易懂]

Python换行符问题:\r\n还是\n?[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-22 10:36:59
1.5K0
发布2022-07-22 10:36:59
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

这是一个很经典的问题。因为不同系统下默认的换行符不同。字符处理时候,这样的“不同”会带来很大的问题,例如line[-2]和line.strip()会因为平台不同返回不同的值。

解决方法:

Python 2

PEP 278 — Universal Newline Support,感谢毕勤的补充):

1)如果不是txt文件,建议用wb和rb来读写。通过二进制读写,不会有换行问题。

2)如果需要明文内容,请用rU来读取(强烈推荐),即U通用换行模式(Universal new line mode)。该模式会把所有的换行符(\r \n \r\n)替换为\n。只支持读入,但是也足够了。这是Python 提供给我们的最好的选择,没有之一。

对比r和rU的结果:

代码语言:javascript
复制
content = file(fn, 'r').read()
# test\r\ntest2
# 这里的换行会因不同系统而不同                       
content = file(fn, 'rU').read()
# test\ntest2
# 所有的换行都被统一,不分系统

Python 3

请注意:Python 3不推荐用rU模式!

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True)

在Python 3,可以通过open函数的newline参数来控制Universal new line mode:读取时候,不指定newline,则默认开启Universal new line mode,所有\n, \r, or \r\n被默认转换为\n ;写入时,不指定newline,则换行符为各系统默认的换行符(\n, \r, or \r\n, ),指定为newline=’\n’,则都替换为\n(相当于Universal new line mode);不论读或者写时,newline=”都表示不转换。

newline controls how universal newlines works (it only applies to text mode). It can be None, ”, ‘\n’, ‘\r’, and ‘\r\n’. It works as follows:

  • On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in ‘\n’, ‘\r’, or ‘\r\n’, and these are translated into ‘\n’ before being returned to the caller. If it is ”, universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
  • On output, if newline is None, any ‘\n’ characters written are translated to the system default line separator,os.linesep. If newline is ”, no translation takes place. If newline is any of the other legal values, any ‘\n’ characters written are translated to the given string.

参考文献:

PEP 278 — Universal Newline Support

Python 3 open: 2. Built-in Functions

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125226.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档