解析文本文件 "r" 与 "rb" 模式的区别(Python)

Difference between parsing a text file in r and rb mode
what’s the differences between r and rb in fopen

0. EOL(End-Of-Line)

区别主要在 EOL 的处理方式不同。对于不同的操作系统而言,

  • Unix:
  • Mac:
  • Windows:

对于 Python 语言,通过如下语句进行查询:

>> import os
>> os.linesep
'
'

1. 不同的操作系统

对于 Windows 系统而言,含有 brbwbr+b) 表示以二进制形式打开文件。windows 下的 Python 对文本文件(text files)和二进制文件(binary files)的处理方式不同,

2. Python 2 vs Python 3

对于 Python 3 环境:

  • r:Python 将会按照编码格式进行解析,read() 操作返回的是str
  • rb:也即 binary mode,read()操作返回的是bytes
原文地址:https://www.cnblogs.com/mtcnn/p/9422226.html