未加星标

Python String splitlines()

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Python String splitlines()

python String splitlines()

Python String splitlines() function returns thelist of lines in the string. The line boundaries are not included in the string, unless we provide keepends argument value as True .

Python String splitlines()

The following table shows the characters that are treated as line boundary by splitlines() function.

Character Description \n Line Feed \r Carriage Return \r\n Carriage Return + Line Feed \v or \x0b Line Tabulation (Python 3.2 onwards) \f or \x0c Form Feed (Python 3.2 onwards) \x1c File Separator \x1d Group Separator \x1e Record Separator \x85 Next Line (C1 Control Code) \u2028 Line Separator \u2029 Paragraph Separator Python String split into lines

Let’s look into some examples of splitlines() function.

Copy

s = 'Java\nPython\nAndroid' lines = s.splitlines() print(lines) lines = s.splitlines(keepends=True) print(lines)

Output:

Copy

['Java', 'Python', 'Android'] ['Java\n', 'Python\n', 'Android']

Let’s look at another example with a combination of line feed and carriage return characters.

Copy

s = 'Java\n\n\n\nPython\n\r\nAndroid' # \r\n is treated as a single line boundary lines = s.splitlines() print(lines) lines = s.splitlines(keepends=True) print(lines)

Output:

Copy

['Java', '', '', '', 'Python', '', 'Android'] ['Java\n', '\n', '\n', '\n', 'Python\n', '\r\n', 'Android']

If you get confused with the output from splitlines() function, test it with keepends as True to understand how the split is performed.

Finally, let’s look into some examples with different separator characters.

Copy

s = 'A\tB\vC\fD' lines = s.splitlines() print(lines) lines = s.splitlines(keepends=True) print(lines) s = 'A\x1cB\x1dC\x1eD\x85E\u2028F\u2029G' lines = s.splitlines() print(lines) lines = s.splitlines(keepends=True) print(lines)

Output:

Copy

['A\tB', 'C', 'D'] ['A\tB\x0b', 'C\x0c', 'D'] ['A', 'B', 'C', 'D', 'E', 'F', 'G'] ['A\x1c', 'B\x1d', 'C\x1e', 'D\x85', 'E\u2028', 'F\u2029', 'G'] Python String split() vs splitlines() We can specify separator insplit() function, splitlines() is only meant to split string into list of lines.

There are slight difference in the split() and splitlines() function working. Let’s see that with examples:

Copy

print("".split('\n')) print("".splitlines()) print("Hi\n".split('\n')) print("Hi\n".splitlines())

Output:

Copy

[''] [] ['Hi', ''] ['Hi']

So with line break as separator, split() returns empty string in the list whereas splitlines() returns empty list.

You can checkout more Python examples from our GitHub Repository .

Official Documentation: partition()

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Python String splitlines()
本站链接:https://www.codesec.net/view/628194.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(143)