添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
愤怒的风衣  ·  能否用JavaScript ...·  3 月前    · 
腼腆的烈马  ·  表单复选框,post ...·  2 年前    · 

python 正则表达式 删除空行

在 python 中,您可以使用正则表达式来删除空行。 您可以使用 re 模块中的 sub() 函数来执行此操作。 下面是一个示例代码:

import re
def remove_empty_lines(text):
    return re.sub(r'\n\s*\n', '\n', text)
text = "This is an example\n\n of removing\n\n empty lines."
print(remove_empty_lines(text))
# Output: "This is an example\n of removing\n empty lines."

在上面的代码中,我们定义了一个名为 remove_empty_lines 的函数,该函数使用正则表达式 r'\n\s*\n' 来搜索文本中的空行,并使用 re.sub() 函数将其删除。

  •