Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.splitlines

bytes — split and join

Split the bytes at line boundaries.

Syntax

b.splitlines(...)

Unlike b.split(b'\n'), this splits on \n, \r and \r\n (treated as one break), and a trailing newline does not add a spurious empty last item. Empty bytes give an empty list rather than a one-element list, so counting lines this way stays honest. There are no other boundaries for bytes: str.splitlines also breaks on \v, \f, \x1c–\x1e and Unicode separators, which bytes leaves inside the line.

Examples

print(b'a\nb'.splitlines())   # → [b'a', b'b']
print(b'a\nb'.splitlines(keepends=True))   # → [b'a\n', b'b']
print(b'a\r\nb'.splitlines())   # → [b'a', b'b']
print(b'a\nb\n'.splitlines())   # → [b'a', b'b']
print(b'a\nb\n'.split(b'\n'))   # → [b'a', b'b', b'']
print(b''.splitlines())   # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy