python - 如何在保留空行的同时拆分?

标签 python string split

例如

'a b\n\n\nc'.split()

给予

['a', 'b', 'c']

在我想要的时候

['a','b','','','c']

有什么想法吗?

最佳答案

使用str.splitlines .检查the docs你会看到它也在其他类似换行符的字符上 split 。如果您希望它\n 上拆分,则使用str.split("\n")

>>> "a\n\n\nb".splitlines()
['a', '', '', 'b']

关于python - 如何在保留空行的同时拆分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041423/

相关文章:

Powershell split() 与 -split - 有什么区别?

python - 用逗号分割以及如何从分割中的引号中排除逗号

java - 解析一行数据: split vs regex

python - Keras 模型到tensorflow.keras

python - '\n' == 'posix' , '\r\n' == 'nt' (python) 对吗?

python - OpenCV-KMeans2导致许多空集群

C++ 字符串比较

c++ - 类中的 ofstream - 试图引用已删除的函数

python - 解析命令调用的输出

python - 获取多对多字段未指向的所有内容