Removing spaces and empty lines from a file Using Python

前端 未结 5 2024
傲寒
傲寒 2020-12-11 05:16

I have a file which contains a value 2000,00.

But it contains spaces after 2000,00 and empty lines.

I want to remove all the spaces and empty lines, if some

5条回答
  •  庸人自扰
    2020-12-11 05:44

    Change your 'lines' line to use the following generator and it should do the trick.

    lines = (line.strip() for line in fh.readlines() if len(line.strip()))
    

提交回复
热议问题