用法:
str.splitlines(keepends=False)返回字符串中的行列表,在行邊界處中斷。換行符不包含在結果列表中,除非給出
keepends並且為真。此方法在以下行邊界上拆分。特別是,邊界是通用換行符的超集。
表示
說明
\n換行
\r回車
\r\n回車 + 換行
\v或\x0b行製表
\f或\x0c換頁
\x1c文件分隔符
\x1d組分隔符
\x1e記錄分隔符
\x85下一行(C1 控製代碼)
\u2028行分隔符
\u2029段落分隔符
在 3.2 版中更改:
\v和\f添加到行邊界列表中。例如:
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n']與給定分隔符字符串
sep時的split()不同,此方法為空字符串返回一個空列表,並且終端換行符不會導致額外的行:>>> "".splitlines() [] >>> "One line\n".splitlines() ['One line']為了比較,
split('\n')給出:>>> ''.split('\n') [''] >>> 'Two lines\n'.split('\n') ['Two lines', '']
相關用法
- Python str.split用法及代碼示例
- Python str.strip用法及代碼示例
- Python str.isidentifier用法及代碼示例
- Python str.expandtabs用法及代碼示例
- Python str.isupper用法及代碼示例
- Python str.title用法及代碼示例
- Python str.rstrip用法及代碼示例
- Python str.lstrip用法及代碼示例
- Python str.removeprefix用法及代碼示例
- Python str.zfill用法及代碼示例
- Python str.format_map用法及代碼示例
- Python str.removesuffix用法及代碼示例
- Python str() vs repr()用法及代碼示例
- Python string轉integer用法及代碼示例
- Python string strip()用法及代碼示例
- Python string.octdigits用法及代碼示例
- Python string.whitespace用法及代碼示例
- Python strip()用法及代碼示例
- Python str()用法及代碼示例
- Python string capitalize()用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 str.splitlines。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
