[Tutor] Space the final frontier

Kent Johnson kent37 at tds.net
Wed Apr 5 12:03:26 CEST 2006


John Corry wrote:
> Your advice was spot on.  I have enclosed the code that I ended up with.
> Instead of appending it to a list, I just wrote it to another file in the
> corrected format.

A few notes below:
> 
> 
> filename = "a:/calllist.csv"
> filename2 = "c:/calllist.csv"
> import string
> import os
> import re
> listy = []
You don't use string, os or listy so these lines can be removed

> input = open( filename, 'r')	#read access
> input2 = open(filename2, 'w')
> for line in input.readlines():
>     line = re.sub('[\s]+', '', line)
>     y = line
>     x = "\n"
>     z = y + x
>     input2.write(z)

I would write either
   line += "\n"
   input2.write(line)

or
   input2.write(line)
   input2.write("\n")

BTW input2 is not such a good name for an output file!
> 
> del input
> del input2

del is not needed here, you should use
   input.close() # not really needed
   input2.close()

I say "not really needed" because for an input file you probably don't 
care exactly when it is closed. For an output file I always close it as 
soon as I am done writing. But maybe I am just teaching my own bad 
habits here!

Kent



More information about the Tutor mailing list