And*_*ner 57 python string line-endings
我在python字符串中有一些包含无关空行的代码.我想从字符串中删除所有空行.什么是最pythonic的方式来做到这一点?
注意:我不是在寻找一般的代码重新格式化程序,只需要快速的一个或两个内容.
谢谢!
Law*_*ton 83
怎么样:
text = os.linesep.join([s for s in text.splitlines() if s])
Run Code Online (Sandbox Code Playgroud)
text带有可能无关的行的字符串在哪里?
Woj*_*ski 15
"\n".join([s for s in code.split("\n") if s])
Run Code Online (Sandbox Code Playgroud)
EDIT2:
text = "".join([s for s in code.splitlines(True) if s.strip("\r\n")])
Run Code Online (Sandbox Code Playgroud)
我认为这是我的最终版本.即使使用代码混合行结尾,它也应该可以正常工作.我不认为带空格的行应该被认为是空的,但如果是这样的话,那么简单的s.strip()就会这样做.
ymv*_*ymv 12
filter(None, code.splitlines())
filter(str.strip, code.splitlines())
Run Code Online (Sandbox Code Playgroud)
相当于
[s for s in code.splitlines() if s]
[s for s in code.splitlines() if s.strip()]
Run Code Online (Sandbox Code Playgroud)
并且可能对可读性有用
小智 10
关于用空间去除NEWLINES和EMPTY LINES的课程
"t"是带有文本的变量.你会看到一个"s"变量,它是一个临时变量,只在评估主要括号的过程中存在(忘了这些lil python东西的名字)
首先设置"t"变量,使其具有新行:
>>> t='hi there here is\na big line\n\nof empty\nline\neven some with spaces\n \nlike that\n\n \nokay now what?\n'
Run Code Online (Sandbox Code Playgroud)
请注意,还有另一种使用三引号设置变量的方法
somevar="""
asdfas
asdf
asdf
asdf
asdf
""""
Run Code Online (Sandbox Code Playgroud)
以下是我们在没有"print"时查看它的样子:
>>> t
'hi there here is\na big line\n\nof empty\nline\neven some with spaces\n \nlike that\n\n \nokay now what?\n'
Run Code Online (Sandbox Code Playgroud)
要查看实际换行符,请将其打印出来.
>>> print t
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
Run Code Online (Sandbox Code Playgroud)
命令删除所有空白线(包括空格):
因此,某些换行符只是新行,有些则有空格,因此它们看起来像新行
如果你想摆脱所有空白行(如果他们只有换行符或空格)
>>> print "".join([s for s in t.strip().splitlines(True) if s.strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
Run Code Online (Sandbox Code Playgroud)
要么:
>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n").strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
Run Code Online (Sandbox Code Playgroud)
注意:t.strip().splitline(True)中的条带可以被删除,因此它只是t.splitlines(True),但是输出可以以额外的换行符结束(这样就删除了最后的换行符).最后一部分s.strip("\ r \n").strip()和s.strip()中的strip()实际上是删除换行符和换行符中的空格.
命令删除所有空白线(但不包括空格):
从技术上讲,带空格的行不应该被认为是空的,但这一切都取决于用例和你想要达到的目的.
>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n")])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
Run Code Online (Sandbox Code Playgroud)
**关于中间带**的说明
那个中间条带,附加到"t"变量,只是删除了最后一个换行符(就像前面的说明所述).如果没有那条带在那里它会是这样的(注意最后一条换行符)
第一个例子(用空格删除换行符和换行符)
>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n").strip()])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
.without strip new line here (stackoverflow cant have me format it in).
Run Code Online (Sandbox Code Playgroud)
第二个例子(仅删除换行符)
>>> print "".join([s for s in t.strip().splitlines(True) if s.strip("\r\n")])
hi there here is
a big line
of empty
line
even some with spaces
like that
okay now what?
.without strip new line here (stackoverflow cant have me format it in).
Run Code Online (Sandbox Code Playgroud)
结束!