本文介绍了使用 Python 的换行符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分隔包含新行的字符串.我将如何实现它?请参考以下代码.

输入:

data = """a,b,cd,e,fg,h,ij,k,l"""

所需的输出:

['a,b,c', 'd,e,f', 'g,h,i', 'j,k,l']

我尝试了以下方法:

1.output = data.split('\n')2. output = data.split('/n')3. output = data.rstrip().split('\n')
解决方案

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

这篇关于使用 Python 的换行符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 13:22