Python换行符处理

Friday, Sep 3, 2021 | 1 minute read | Updated at Friday, Sep 3, 2021

@
Python换行符处理

python换行符处理

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的结果:

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

Python 3(Open函数: 2. Built-in Functions)

** 请注意: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=‘‘都表示不转换。

© 2016 - 2025 Caisong's Blog

🌱 Powered by Hugo with theme Dream.

About Me

大龄程序员,喜欢折腾各种环境部署、软件应用。

博客记录日常。