Python splitlines()方法

Python 教程 · 2019-02-02 17:57:54

描述

Python splitlines() 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

语法

splitlines()方法语法:

str.splitlines([keepends])

参数

  • keepends -- 在输出结果里是否保留换行符('\r', '\r\n', \n'),默认为 False,不包含换行符,如果为 True,则保留换行符。

返回值

返回一个包含各行作为元素的列表。

实例

以下实例展示了splitlines()函数的使用方法:

实例(Python 2.0+)

#!/usr/bin/python str1 = 'ab c\n\nde fg\rkl\r\n' print str1.splitlines(); str2 = 'ab c\n\nde fg\rkl\r\n' print str2.splitlines(True)

以上实例输出结果如下:

['ab c', '', 'de fg', 'kl']
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Learning Python, 5th Edition

Learning Python, 5th Edition

Mark Lutz / O'Reilly Media / 2013-7-6 / USD 64.99

If you want to write efficient, high-quality code that's easily integrated with other languages and tools, this hands-on book will help you be productive with Python quickly. Learning Python, Fifth Ed......一起来看看 《Learning Python, 5th Edition》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具