Python 的 str.splitlines(~) 方法在換行符處拆分字符串,並返回包含分隔行的列表。
下麵列表中的表示被視為換行符:
|
表示 |
說明 |
|---|---|
|
|
換行 |
|
|
回車 |
|
|
回車 + 換行 |
|
|
行製表 |
|
|
換頁 |
|
|
文件分隔符 |
|
|
組分隔符 |
|
|
記錄分隔符 |
|
|
下一行(C1 控製代碼) |
|
|
行分隔符 |
|
|
段落分隔符 |
參數
1.keepends | boolean | optional
換行符本身是否包含在返回的列表中。默認情況下,False(即不包括)。
返回值
分隔行的列表。
例子
基本用法
要在換行符處拆分字符串 "abc\n e f\rg":
x = "abc\n e f\rg"
x.splitlines()
['abc', ' e f', 'g']
我們可以看到該字符串被分成兩部分(一次在 \n ,一次在 \r )。
保留參數
要在換行符處拆分字符串 "abc\n e f\rg" 並將換行符包含在返回的列表中:
y = "abc\n e f\rg"
y.splitlines(True)
['abc\n', ' e f\r', 'g']
我們可以看到返回的列表包含換行符 \n 和 \r 。
相關用法
- Python String splitlines()用法及代碼示例
- Python String split方法用法及代碼示例
- Python String split()用法及代碼示例
- Python String startswith()用法及代碼示例
- Python String strip方法用法及代碼示例
- Python String strip()用法及代碼示例
- Python String swapcase()用法及代碼示例
- Python String swapcase方法用法及代碼示例
- Python String startswith方法用法及代碼示例
- Python String count方法用法及代碼示例
- Python String isnumeric方法用法及代碼示例
- Python String Center()用法及代碼示例
- Python String zfill方法用法及代碼示例
- Python String rstrip方法用法及代碼示例
- Python String decode()用法及代碼示例
- Python String count()用法及代碼示例
- Python String join()用法及代碼示例
- Python String casefold()用法及代碼示例
- Python String isalnum()用法及代碼示例
- Python String endswith方法用法及代碼示例
- Python String rsplit()用法及代碼示例
- Python String isidentifier()用法及代碼示例
- Python String rjust方法用法及代碼示例
- Python String rpartition()用法及代碼示例
- Python String rpartition方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Python String | splitlines method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
