Split string using a newline delimiter with Python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands
--
Chapters
00:00 Question
00:39 Accepted answer (Score 284)
00:56 Answer 2 (Score 15)
01:42 Answer 3 (Score 15)
02:19 Answer 4 (Score 7)
02:33 Thank you
--
Full question
https://stackoverflow.com/questions/2204...
Accepted answer links:
[str.splitlines]: http://docs.python.org/library/stdtypes....
Answer 2 links:
[str.splitlines()]: http://www.tutorialspoint.com/python/str...
[str.split()]: https://www.w3schools.com/python/ref_str...
Answer 3 links:
[@Ashwini Chaudhary suggested in the comments]: https://stackoverflow.com/questions/2204...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #python27
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands
--
Chapters
00:00 Question
00:39 Accepted answer (Score 284)
00:56 Answer 2 (Score 15)
01:42 Answer 3 (Score 15)
02:19 Answer 4 (Score 7)
02:33 Thank you
--
Full question
https://stackoverflow.com/questions/2204...
Accepted answer links:
[str.splitlines]: http://docs.python.org/library/stdtypes....
Answer 2 links:
[str.splitlines()]: http://www.tutorialspoint.com/python/str...
[str.split()]: https://www.w3schools.com/python/ref_str...
Answer 3 links:
[@Ashwini Chaudhary suggested in the comments]: https://stackoverflow.com/questions/2204...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #python27
#avk47
ACCEPTED ANSWER
Score 294
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']
ANSWER 2
Score 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())
ANSWER 3
Score 7
There is a method specifically for this purpose:
data.splitlines()
['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
ANSWER 4
Score 3
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']
>>>