Split a string at newline characters
Asked Answered
R

6

18

I have a string, say

a = "Show  details1\nShow  details2\nShow  details3\nShow  details4\nShow  details5\n"

How do we split the above with the delimiter \n (a newline)?

The result should be

['Show  details1', 'Show  details2', ..., 'Show  details5']
Reexamine answered 19/1, 2010 at 14:38 Comment(5)
do you want it to be ["show", "details1", "show", "details2", ... ] or ["show details1", "show details2", ... ]Bathysphere
Is it literally '\n', or is it a newline character?Steelwork
@ezod: \n is a new-line characterOtes
@Hulk: where are you getting this line from?Otes
I need it as ["show details1", "show details2", ... ] Also i am getting the above line from a log....Reexamine
M
18

If you are concerned only with the trailing newline, you can do:

a.rstrip().split('\n')

See, str.lstrip() and str.strip() for variations.

If you are more generally concerned by superfluous newlines producing empty items, you can do:

filter(None, a.split('\n'))
Myles answered 19/1, 2010 at 15:59 Comment(0)
L
22

Use a.splitlines(). This will return you a list of the separate lines. To get your "should be" result, add " ".join(a.splitlines()), and to get all in lower case as shown, the whole enchilada looks like " ".join(a.splitlines()).lower().

Lafond answered 19/1, 2010 at 14:41 Comment(4)
Paul, I integrated info from the OP's comment to another answer. He doesn't seem to want the "join" part of this...Compartmentalize
@Idan, it's lots fast. On my machine it splits the 10MB string 'test this\n'*int(1e6)` in 280ms or 28ns per char. Do you have a performance problem involving this function?Compartmentalize
@Peter - Thanks, no problems. Was just wondering since it does 2 passes on the original string (one for splitlines and another for lower) but I guess it doesn't change much.Sing
This is a much better response then most answers to most questions of this kind, because it also works with windows style newlines.Particularity
M
18

If you are concerned only with the trailing newline, you can do:

a.rstrip().split('\n')

See, str.lstrip() and str.strip() for variations.

If you are more generally concerned by superfluous newlines producing empty items, you can do:

filter(None, a.split('\n'))
Myles answered 19/1, 2010 at 15:59 Comment(0)
O
6

split method:

a.split('\n')[:-1]
Otes answered 19/1, 2010 at 14:39 Comment(0)
A
1
 a.split('\n')

would return an empty entry as the last member of the list.so use

a.split('\n')[:-1]

Arango answered 19/1, 2010 at 16:12 Comment(0)
C
0

try:

a.split('\n')
Cult answered 19/1, 2010 at 14:40 Comment(2)
This doesnt work. i get the result as ['Show details', 'Show details', ''] 1 more elementReexamine
So ignore the last element? Your input string does have an empty string after the last \n.Pursuer
B
0

Had a similar issue. I was getting csv data in this format

["User Name\r\nEmail\r\nContact\nAddress", "Name 1\r\[email protected]\r\n+12345657890\r\nSample Address", "Name 2\r\[email protected]\r\n+21345657890\r\nSample Address 2", ]

I used [i.splitlines() for i n data] to convert in list of list

>>> data = ["User Name\r\nEmail\r\nContact\nAddress", "Name 1\r\[email protected]\r\n+12345657890\r\nSample Address", "Name 2\r\[email protected]\r\n+21345657890\r\nSample Address 2", ]
>>> [i.splitlines() for i in data]
[['User Name', 'Email', 'Contact', 'Address'], ['Name 1', '[email protected]', '+12345657890', 'Sample Address'], ['Name 2', '[email protected]', '+21345657890', 'Sample Address 2']]
Borst answered 6/5 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.