''' Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串 ''' str='"Line1-abcdef \nLine2-abc \nLine4-abcd"' print(str.split()) print(str.split(' ',0)) print(str.split(' ',1)) print(str.split('a')) ''' ['"Line1-abcdef', 'Line2-abc', 'Line4-abcd"'] ['"Line1-abcdef \nLine2-abc \nLine4-abcd"'] ['"Line1-abcdef', '\nLine2-abc \nLine4-abcd"'] ['"Line1-', 'bcdef \nLine2-', 'bc \nLine4-', 'bcd"'] ''' ''' Python splitlines() 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。 ''' lines = ''' dog cat bird buffalo gophers hound horse '''.strip().splitlines() print(lines) ''' ['dog', 'cat', 'bird', 'buffalo', 'gophers', 'hound', 'horse'] ''' lines1 = ''' dog cat bird buffalo gophers hound horse '''.splitlines() print(lines1) ''' ['', 'dog', 'cat', 'bird', 'buffalo', 'gophers', 'hound', 'horse'] '''
python str.split str.splitlines 字符串分割返回列表
最新推荐文章于 2024-01-12 20:00:00 发布