Removing spaces and empty lines from a file Using Python

前端 未结 5 2031
傲寒
傲寒 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 06:01

    This should work as you wish:

    file(filename_out, "w").write(file(filename_in).read().strip())
    

    EDIT: Although previous code works in python 2.x, it does not work python 3 (see @gnibbler comment) For both version use this:

    open(filename_out, "w").write(open(filename_in).read().strip())
    

提交回复
热议问题