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,cd,e,fg,h,ij,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')
5 个解决方案
#1
96
str.splitlines
method should give you exactly that.
splitlines方法应该会给出这样的结果。
>>> 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']
#2
7
data = """a,b,cd,e,fg,h,ij,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
默认情况下,split是由所有空格字符分割的。如果实际的字符串有任何其他空格字符,您可能希望使用
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
或者正如@Ashwini Chaudhary在评论中建议的那样,您可以使用它
print(data.splitlines())
#3
5
If you want to split only by newlines, its better to use splitlines():
如果你只想用换行符分割,最好使用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 split() it works also:
split()也可以:
>>> 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:
然而:
>>> data = """... a, eqw, qwe... v, ewr, err... """>>> data'\na, eqw, qwe\nv, ewr, err\n'>>> data.split()['a,', 'eqw,', 'qwe', 'v,', 'ewr,', 'err']
#4
4
There is a method specifically for this purpose:
有一种专门为此目的的方法:
data.splitlines()['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
#5
2
Here you go:
给你:
>>> data = """a,b,cd,e,fg,h,ij,k,l""">>> data.split() # split automatically splits through \n and space['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']>>>
#1
96
str.splitlines
method should give you exactly that.
splitlines方法应该会给出这样的结果。
>>> 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']
#2
7
data = """a,b,cd,e,fg,h,ij,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
默认情况下,split是由所有空格字符分割的。如果实际的字符串有任何其他空格字符,您可能希望使用
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
或者正如@Ashwini Chaudhary在评论中建议的那样,您可以使用它
print(data.splitlines())
#3
5
If you want to split only by newlines, its better to use splitlines():
如果你只想用换行符分割,最好使用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 split() it works also:
split()也可以:
>>> 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:
然而:
>>> data = """... a, eqw, qwe... v, ewr, err... """>>> data'\na, eqw, qwe\nv, ewr, err\n'>>> data.split()['a,', 'eqw,', 'qwe', 'v,', 'ewr,', 'err']
#4
4
There is a method specifically for this purpose:
有一种专门为此目的的方法:
data.splitlines()['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']
#5
2
Here you go:
给你:
>>> data = """a,b,cd,e,fg,h,ij,k,l""">>> data.split() # split automatically splits through \n and space['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']>>>