[CS-FSLUG] An easy way to remove blank lines?

Jukka jukka.ylonen at gmail.com
Sat Sep 10 20:31:32 CDT 2005


2005/9/10, Chris Brault <groundhog3000 at yahoo.com>:
> After some corrections,
> 
> I've tested the following code and it works well:
> 
> 
> #---------------------------------------------------
> #!/usr/bin/python
> 
> import os, os.path, re, sys
> 
> # First we find the file name from the input
> file_name = sys.argv[1]
> 
> # If the file exist, then we extract the lines from it
> if os.path.isfile(file_name) and
> os.path.exists(file_name):
>     file_reference = file(file_name, 'r')
>     file_lines = file_reference.readlines()
>     file_reference.close()
>     new_lines = file_lines
> 
> # Run through the lines of the file and erase blank
> ones
> for line in file_lines:
>      if re.findall("\S", line) == []:
>          print line
>          print "LENGTH:", len(line), "FINDALL:",
> re.findall("\S", line)
>          new_lines.remove(line)
> 
> # Rewrite the file without the blank lines
> file_reference = file(file_name, 'w')
> file_reference.writelines(new_lines)
> file_reference.close()
> 
> #---------------------------------------------------
> 
> 
> Gabe
> 

Hi Gabe!

I managed to get it working also. I think I had problems probably
because some lines were splitted in email - after removing two line
breaks it started to work. With this all my NVU-generated html-files
are more readable. :)

Thank you,
Jukka




More information about the Christiansource mailing list