splitlines in Python
*Memos:
- My post explains split() with a string and byte string.
- My post explains rsplit() with a string and byte string.
- My post explains partition() with a string and byte string.
- My post explains rpartition() with a string and byte string.
- My post explains encode() and decode() with a string and byte string.
- My post explains a string.
str.splitlines() and bytes.splitlines() or bytearray.splitlines() can split a string and byte string respectively at one or more line boundaries as shown below:
*Memos:
- The 1st argument is
keepends
(Optional-Default:False
-Type:bool
). *Ifkeepends
isTrue
, one or more line boundaries are included otherwise they aren’t included. - These below are line boundaries:
n |
Line Feed |
r |
Carriage Return |
rn |
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 |
<String>:
v1 = '1 onen2 twor3 threern4 fourv5 five'
v2 = '1 onex0b2 twof3 threex0c4 four'
v3 = '1 onex1c2 twox1d3 threex1e4 four'
v4 = '1 onex852 twou20283 threeu20294 four'
print(v1)
# 1 one
# 3 three
# 4 four5 five
print(v1.splitlines())
print(v1.splitlines(keepends=False))
# ['1 one', '2 two', '3 three', '4 four', '5 five']
print(v1.splitlines(keepends=True))
# ['1 onen', '2 twor', '3 threern', '4 fourx0b', '5 five']
print(v2)
# 1 one2 two3 three4 four
print(v2.splitlines())
# ['1 one', '2 two', '3 three', '4 four']
print(v2.splitlines(keepends=True))
# ['1 onex0b', '2 twox0c', '3 threex0c', '4 four']
print(v3)
# 1 one2 two3 three4 four
print(v3.splitlines())
# ['1 one', '2 two', '3 three', '4 four']
print(v3.splitlines(keepends=True))
# ['1 onex1c', '2 twox1d', '3 threex1e', '4 four']
print(v4)
# 1 one…2 two
3 three
4 four
print(v4.splitlines())
# ['1 one', '2 two', '3 three', '4 four']
print(v4.splitlines(keepends=True))
# ['1 onex85', '2 twou2028', '3 threeu2029', '4 four']
v = '1 one 2 two 3 three 4 four 5 five'
print(v)
# 1 one 2 two 3 three 4 four 5 five
print(v.splitlines())
print(v.splitlines(keepends=True))
# ['1 one 2 two 3 three 4 four 5 five']
v = ''
print(v.splitlines())
# []
<Byte String(UTF-8)>:
bytes():
v1 = '1 onen2 twor3 threern4 fourv5 five'.encode()
v1 = b'1 onen2 twor3 threern4 fourx0b5 five'
v2 = '1 onex0b2 twof3 threex0c4 four'.encode()
v2 = b'1 onex0b2 twox0c3 threex0c4 four'
v3 = '1 onex1c2 twox1d3 threex1e4 four'.encode()
v3 = b'1 onex1c2 twox1d3 threex1e4 four'
v4 = '1 onex852 twou20283 threeu20294 four'.encode()
v4 = b'1 onexc2x852 twoxe2x80xa83 threexe2x80xa94 four'
print(v1)
# b'1 onen2 twor3 threern4 fourx0b5 five'
print(v1.splitlines())
print(v1.splitlines(keepends=False))
# [b'1 one', b'2 two', b'3 three', b'4 fourx0b5 five']
print(v1.splitlines(keepends=True))
# [b'1 onen', b'2 twor', b'3 threern', b'4 fourx0b5 five']
print(v2)
# b'1 onex0b2 twox0c3 threex0c4 four'
print(v2.splitlines())
print(v2.splitlines(keepends=True))
# [b'1 onex0b2 twox0c3 threex0c4 four']
print(v3)
# b'1 onex1c2 twox1d3 threex1e4 four'
print(v3.splitlines())
print(v3.splitlines(keepends=True))
# [b'1 onex1c2 twox1d3 threex1e4 four']
print(v4)
# b'1 onexc2x852 twoxe2x80xa83 threexe2x80xa94 four'
print(v4.splitlines())
print(v4.splitlines(keepends=True))
# [b'1 onexc2x852 twoxe2x80xa83 threexe2x80xa94 four']
v = '1 one 2 two 3 three 4 four 5 five'.encode()
v = b'1 one 2 two 3 three 4 four 5 five'
print(v)
# b'1 one 2 two 3 three 4 four 5 five'
print(v.splitlines())
print(v.splitlines(keepends=True))
# [b'1 one 2 two 3 three 4 four 5 five']
v = ''.encode()
v = b''
print(v.splitlines())
# []
bytearray():
v1 = bytearray('1 onen2 twor3 threern4 fourv5 five'.encode())
v2 = bytearray('1 onex0b2 twof3 threex0c4 four'.encode())
v3 = bytearray('1 onex1c2 twox1d3 threex1e4 four'.encode())
v4 = bytearray('1 onex852 twou20283 threeu20294 four'.encode())
print(v1)
# bytearray(b'1 onen2 twor3 threern4 fourx0b5 five')
print(v1.splitlines())
print(v1.splitlines(keepends=False))
# [bytearray(b'1 one'), bytearray(b'2 two'), bytearray(b'3 three'),
# bytearray(b'4 fourx0b5 five')]
print(v1.splitlines(keepends=True))
# [bytearray(b'1 onen'), bytearray(b'2 twor'),
# bytearray(b'3 threern'), bytearray(b'4 fourx0b5 five')]
print(v2)
# bytearray(b'1 onex0b2 twox0c3 threex0c4 four')
print(v2.splitlines())
print(v2.splitlines(keepends=True))
# [bytearray(b'1 onex0b2 twox0c3 threex0c4 four')]
print(v3)
# bytearray(b'1 onex1c2 twox1d3 threex1e4 four')
print(v3.splitlines())
print(v3.splitlines(keepends=True))
# [bytearray(b'1 onex1c2 twox1d3 threex1e4 four')]
print(v4)
# bytearray(b'1 onexc2x852 twoxe2x80xa83 threexe2x80xa94 four')
print(v4.splitlines())
print(v4.splitlines(keepends=True))
# [bytearray(b'1 onexc2x852 twoxe2x80xa83 threexe2x80xa94 four')]
v = bytearray('1 one 2 two 3 three 4 four 5 five'.encode())
print(v)
# bytearray(b'1 one 2 two 3 three 4 four 5 five')
print(v.splitlines())
print(v.splitlines(keepends=True))
# [bytearray(b'1 one 2 two 3 three 4 four 5 five')]
v = bytearray(''.encode())
print(v.splitlines())
# []