all Python 3Byte sequencesfunction
bytearray.splitlines
bytearray — split and join
Split the bytearray into a list of lines at line boundaries; keepends=True keeps the line breaks.
Syntax
ba.splitlines(...)
Unlike split(b'\n'), a trailing newline does not produce a spurious empty last element, and an empty bytearray yields an empty list — which is why line-by-line parsing uses splitlines. For byte data only \n, \r and \r\n count as boundaries (str recognises considerably more), and keepends=True keeps the terminator attached to each line.
Examples
print(bytearray(b'a\nb').splitlines()) # → [bytearray(b'a'), bytearray(b'b')]
print(bytearray(b'a\nb').splitlines(True)) # → [bytearray(b'a\n'), bytearray(b'b')]
print(bytearray(b'a\r\nb\rc').splitlines()) # → [bytearray(b'a'), bytearray(b'b'), bytearray(b'c')]
print(len(bytearray(b'line1\nline2\nline3').splitlines())) # → 3
print(bytearray(b'a\nb\n').splitlines()) # → [bytearray(b'a'), bytearray(b'b')]
print(bytearray(b'a\nb\n').split(b'\n')) # → [bytearray(b'a'), bytearray(b'b'), bytearray(b'')]
See also
Open in the full glossarydocs.python.orgReport an inaccuracy