How to remove empty lines with or without whitespace in Python

后端 未结 10 2450
感动是毒
感动是毒 2020-12-01 07:42

I have large string which I split by newlines. How can I remove all lines that are empty, (whitespace only)?

pseudo code:

for stuff in largestring:
          


        
10条回答
  •  Happy的楠姐
    2020-12-01 07:58

    I also tried regexp and list solutions, and list one is faster.

    Here is my solution (by previous answers):

    text = "\n".join([ll.rstrip() for ll in original_text.splitlines() if ll.strip()])
    

提交回复
热议问题