从 python 字符串中删除空行的快速单行是什么? [英] What's a quick one-liner to remove empty lines from a python string?
本文介绍了从 python 字符串中删除空行的快速单行是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在包含无关空行的 python 字符串中有一些代码.我想从字符串中删除所有空行.执行此操作的最 Pythonic 方法是什么?
I have some code in a python string that contains extraneous empty lines. I would like to remove all empty lines from the string. What's the most pythonic way to do this?
注意:我不是在寻找通用的代码重新格式化程序,只是一个快速的一两行代码.
Note: I'm not looking for a general code re-formatter, just a quick one or two-liner.
谢谢!
推荐答案
怎么样:
text = os.linesep.join([s for s in text.splitlines() if s])
其中 text
是包含可能的无关行的字符串?
where text
is the string with the possible extraneous lines?
这篇关于从 python 字符串中删除空行的快速单行是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文