ShowOwi
g exsu n
Sierouz
nicee99
这是我使用tesseract.The空格执行ocr后得到的输出,在某些情况下,用户名之间有多个空格,有时用户名之间没有空格,我正在试图找到一种解决方案,它会去掉这些空白行,.I在写到file.How之前想要去掉它们,只删除空白,但使用python将它们保留在另一个空格下面。
在这方面,我也经历过类似的问题,但对我来说似乎没有两样东西。
发布于 2017-09-12 02:55:23
简单地过滤你的行怎么样:
lines = output.splitlines()
filtered = [line for line in lines if line.strip()]
if line.strip()
隐式地检查line.strip() != ""
(空字符串是falsy值)。
当然,这也可以使用功能方式来完成:
filtered = filter(lambda line: line.strip(), lines)
要拿回一根绳子:
new_output = '\n'.join(filtered)
发布于 2017-09-12 03:03:30
简单地使用re.sub()
函数:
import re
s = '''
ShowOwi
g exsu n
Sierouz
nicee99
'''
result = re.sub('\n+', '\n', s.strip())
print(result)
产出:
ShowOwi
g exsu n
Sierouz
nicee99
https://stackoverflow.com/questions/46174526
复制相似问题