且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Python 的换行符分割字符串

更新时间:2023-11-13 22:54:34

str.splitlines 方法应该给你.

>>>数据 = """a,b,c... d,e,f... g,h,i... j,k,l""">>>数据分割线()['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

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')

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']