python - How to remove blank lines only and retain the order -


showowi  g exsu n  sierouz  nicee99 

this output after performing ocr using tesseract.the blank space varies in occurence times there multiple blanks between usernames none between usernames.i trying find solution strip these blank lines .i want strip them before writing file.how remove blank space retain them 1 below other using python.

i went through similar questions in regarding nothing seems tow work me.

what filtering lines:

lines = output.splitlines() filtered = [line line in lines if line.strip()] 

the if line.strip() implictly check line.strip() != "" (empty string falsy value).

of course, can done using functional way:

filtered = filter(lambda line: line.strip(), lines) 

and single string back:

new_output = '\n'.join(filtered) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -