162

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')
Cœur
  • 37,241
  • 25
  • 195
  • 267
Hariharan
  • 3,191
  • 4
  • 19
  • 31

7 Answers7

288

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']
phoenix
  • 7,988
  • 6
  • 39
  • 45
wim
  • 338,267
  • 99
  • 616
  • 750
  • 10
    One convenient part about `str.splitlines` is that it will remove the final `\n` if its present. I.e, `'foo\nbar\n'.split() == ['foo', 'bar', '']` while `str.splitlines('foo\nbar\n') == ['foo', 'bar']` – Matthew Moisen Feb 15 '17 at 22:25
15
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())
Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
15

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']
KiraLT
  • 2,385
  • 1
  • 24
  • 36
  • 2
    seems strange that you don't have to pass the character to split by like `data.split('\n')`? –  Mar 10 '19 at 09:01
7

There is a method specifically for this purpose:

data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
pajton
  • 15,828
  • 8
  • 54
  • 65
4

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']
>>> 
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
0

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

0

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.

hitesh bedre
  • 459
  • 2
  • 11