Remove empty lines

Bengt Richter bokr at oz.net
Wed Mar 20 13:13:42 EST 2002


On Wed, 20 Mar 2002 18:00:21 +0100, Kristin Skoglund <krists at ifi.uio.no> wrote:

>Hi
>
>I am trying to make a script that reads javafiles and remove comments
>and empty lines. The script looks like this......
>
>def removestuff(file):
>
>    infilename=file
>
>    infile=open(infilename, 'r')
>
>    copyfile=infilename +".bak"
>
>    newfile=open(copyfile,'w')
>
>    str = infile.read()
>
>    str=re.sub(r"/\*\*\n.*\n*\*/|/\*\n.*|\*.*|\s*/\s*"," ",str) #This strips away comments
>
>    lines=re.split("\n",str)
>
>    for line in lines:   # Here I try to remove the emty lines
>
>        if line== '':
>
>            continue
>
>        elif empty:
>
>            continue
>
>        else:
>
>            tmp.append(line)
>
>            newfile.write(line)
>
>    infile.close()
>
>    newfile.close()
>
>...and everything works except the removal of empty lines. Does anyone
>knoe whats wrong?
>
What do you see if you do a hex char dump of the new file, where it
has "empty" lines?

Maybe you need to match on all-white-space instead of '' after splitting with \n ?

Regards,
Bengt Richter




More information about the Python-list mailing list