新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonsplitlines()
python 中的splitlines()函數(shù)有助于返回字符串中的行列表,這里的拆分是在換行符處完成的。它以布爾值作為參數(shù)。

**str.splitlines([keepends])** #where keepends is a boolean value
分割線()參數(shù):
splitlines()函數(shù)接受一個(gè)參數(shù)。默認(rèn)情況下,不提供換行符。換行符也包含在列表的項(xiàng)目中,它可以是以下任何一種。
| \n | 換行 | | \r | 回車 | | \r\n | 回車+換行 | | \v 或 \x0b | 行列表 | | \f 或 \x0c | 換頁 | | \x1c | 文件分隔符 | | \x1d | 分組分隔符 | | \x1e | 記錄分離器 | | \x85 | 下一行(C1 控制代碼) | | \u2028 | 行分隔符 | | \u2029 | 段落分隔符 |
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 周末 | 指定是否應(yīng)包含換行符(真)或不包含換行符(假) | 可選擇的 |
分割線()返回值
如果沒有給定換行符,它將返回一個(gè)包含單個(gè)項(xiàng)目(單行)的列表。
| 投入 | 返回值 | | 如果 keepends | 字符串中的行列表 |
Python 中splitlines()方法的示例
示例splitlines()在 Python 中是如何工作的?
fruits = 'Apple\Orange\r\nBanana\rGrapes'
print(fruits .splitlines())
print(fruits .splitlines(True))
fruits = 'Apple Orange Banana Grapes'
print(fruits.splitlines())
輸出:
['Apple', 'Orange', 'Banana', 'Grapes']
['Apple\n', 'Orange\r\n', 'Banana\r', 'Grapes']
['Apple Orange Banana Grapes']
示例 2:在 Python 中使用分割線()
string = "Hi How are you\nIam fine.."
output = string.splitlines(True)
print(output)
輸出:
['Hi How are you\n', 'Iam fine..'] 網(wǎng)站名稱:創(chuàng)新互聯(lián)Python教程:Pythonsplitlines()
標(biāo)題URL:http://www.dlmjj.cn/article/djicsds.html


咨詢
建站咨詢
