How do you count text records across newline styles without an extra trailing row?

splitlines separates text at recognized line boundaries, strips terminators by default and adds no extra row for a final terminator.

Python example and interpretation

A sample with CRLF, a blank line and LF retains the interior blank. Let a CSV parser find records when quoted fields may contain line breaks.

'a\r\n\r\nb\n'.splitlines()

Result ['a', '', 'b']

中文

文本样本怎样兼容多种换行符且不多算末尾空行?

splitlines 根据行边界拆分文本,默认去掉边界字符,末尾单个换行不生成额外一行。

Python 算例与适用条件

含 CRLF、空白行和 LF 的样本会保留中间空行。遇到允许引号内换行的 CSV,应由 CSV 解析器识别记录。

'a\r\n\r\nb\n'.splitlines()

结果 ['a', '', 'b']

References / 参考来源

Keep reading / 继续阅读