def strip_empty_lines(s):
lines = s.splitlines()
while lines and not lines[0].strip():
lines.pop(0)
while lines and not lines[-1].strip():
lines.pop()
return '\n'.join(lines)
这将处理除\n行分隔符之外的“空行”仍包含空格或制表符的情况:
^{pr2}$
如果除了换行符之外没有其他空格,那么一个简单的s.strip('\n')就可以:
>>> '''\
...
...
...
... some indentation here
...
... lorum ipsum
...
... '''.strip('\n')
' some indentation here\n\nlorum ipsum'
收益率
^{pr2}$你必须使用自定义解决方案。用换行符拆分行,并从开始和结束处删除空行:
这将处理除
^{pr2}$\n
行分隔符之外的“空行”仍包含空格或制表符的情况:如果除了换行符之外没有其他空格,那么一个简单的
s.strip('\n')
就可以:相关问题 更多 >
编程相关推荐