Python splitlines() 函数的坑

#str.splitlines()

Python 标准库中的 str.splitlines() 会在以下几种字符处分割字符串,不仅限于 universal newlines (\r,\n,\r\n):

RepresentationDescription
\nLine Feed
\rCarriage Return
\r\nCarriage Return + Line Feed
\v or \x0bLine Tabulation
\f or \x0cForm Feed
\x1cFile Separator
\x1dGroup Separator
\x1eRecord Separator
\x85Next Line (C1 Control Code)
\u2028Line Separator
\u2029Paragraph 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参数的值