python换行符 n怎么用_Python换行符问题:\r\n还是\n?

本文介绍了Python中不同系统下换行符的区别,包括Windows的 、Unix的 和Mac的 。强调了在处理文本时,这种差异可能导致的问题。提出了解决方案,如在Python 2中使用rU模式进行读取,而在Python 3中通过open函数的newline参数来控制Universal new line mode,确保跨平台的换行符一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

章硕,js & python

夏一一、乔3少 赞同

这不是python的问题,windows的换行是\r\n,unix的是\n,mac的是\r。

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

解决方法:

Python 2

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

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

对比r和rU的结果:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值