Split string using a newline delimiter with Python
PythonStringPython 2.7Python Problem Overview
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')
Python Solutions
Solution 1 - Python
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']
Solution 2 - Python
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,c\nd,e,f\ng,h,i\nj,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,c\nd,e,f\ng,h,i\nj,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, qwe\nv, ewr, err\n'
>>> data.split()
['a,', 'eqw,', 'qwe', 'v,', 'ewr,', 'err']
Solution 3 - Python
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())
Solution 4 - Python
There is a method specifically for this purpose:
data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
Solution 5 - Python
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']
>>>
Solution 6 - Python
Since the split gets a string as separator you should have additional back slash output = data.split('\n')
Solution 7 - Python
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.