Python splitlines() 函数的坑
#str.splitlines()
Python 标准库中的 str.splitlines() 会在以下几种字符处分割字符串,不仅限于 universal newlines (\r,\n,\r\n):
| Representation | Description | 
|---|---|
\n | Line Feed | 
\r | Carriage Return | 
\r\n | Carriage Return + Line Feed | 
\v or \x0b | Line Tabulation | 
\f or \x0c | Form Feed | 
\x1c | File Separator | 
\x1d | Group Separator | 
\x1e | Record Separator | 
\x85 | Next Line (C1 Control Code) | 
\u2028 | Line Separator | 
\u2029 | Paragraph Separator | 
#bytes.splitlines()
bytes.splitlines() 严格按照 universal newlines 分割
#readlines()
readlines() 的行为和 open() 时 newline 参数的值有关。
| open(newline=…) | 输入行为 | 输出行为 | 
|---|---|---|
| None | 按照 universal newlines 分割,且会被自动转换为 \n | 输出中的 \n 会被自动转换成 os.linesep | 
'' | 按照 universal newlines 分割,不会进行转换 | 不会进行转换 | 
'\n', '\r', '\r\n' | 按照 newline 分割,不会进行转换 | 输出中的 \n 会被自动转换成 newline参数的值 |