Split string using a newline delimiter with Python

I need to delimit the string which has new line in it. How would I achieve it? Please refer below code.

Input:

data = """a,b,c
d,e,f
g,h,i
j,k,l"""

Output desired:

['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

I have tried the below approaches:

1. output = data.split('n')
2. output = data.split('/n')
3. output = data.rstrip().split('n')

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

str.splitlines method should give you exactly that.

>>> data = """a,b,c
... d,e,f
... g,h,i
... j,k,l"""
>>> data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

Method 2

data = """a,b,c
d,e,f
g,h,i
j,k,l"""

print(data.split())       # ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

str.split, by default, splits by all the whitespace characters. If the actual string has any other whitespace characters, you might want to use

print(data.split("n"))   # ['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

Or as @Ashwini Chaudhary suggested in the comments, you can use

print(data.splitlines())

Method 3

If you want to split only by newlines, you can use str.splitlines():

Example:

>>> data = """a,b,c
... d,e,f
... g,h,i
... j,k,l"""
>>> data
'a,b,cnd,e,fng,h,inj,k,l'
>>> data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

With str.split() your case also works:

>>> data = """a,b,c
... d,e,f
... g,h,i
... j,k,l"""
>>> data
'a,b,cnd,e,fng,h,inj,k,l'
>>> data.split()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

However if you have spaces (or tabs) it will fail:

>>> data = """
... a, eqw, qwe
... v, ewr, err
... """
>>> data
'na, eqw, qwenv, ewr, errn'
>>> data.split()
['a,', 'eqw,', 'qwe', 'v,', 'ewr,', 'err']

Method 4

There is a method specifically for this purpose:

data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

Method 5

Here you go:

>>> data = """a,b,c
d,e,f
g,h,i
j,k,l"""
>>> data.split()  # split automatically splits through n and space
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
>>>

Method 6

Since the split gets a string as separator you should have additional back slash
output = data.split(‘n’)

Method 7

We can also use regex’s split method too.

import re

data = """a,b,c
d,e,f
g,h,i
j,k,l"""

output = re.split("n", data)

print(output) #['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

Hope this will help somebody.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x