[Tutor] Space the final frontier
Alan Gauld
alan.gauld at freenet.co.uk
Wed Apr 5 13:41:56 CEST 2006
> Thanks for the prompt replies. I have now processed my file with 999
> lines
> and it took seconds. It was beautiful.
Glad it works, a couple of tweaks:
> filename = "a:/calllist.csv"
> filename2 = "c:/calllist.csv"
> import string
> import os
You don't use os or string so don't need to import them
> import re
And you don't really need re either, see below...
> listy = []
> input = open( filename, 'r') #read access
> input2 = open(filename2, 'w')
input is a builtin function so using it as a variable name could
cause confusion - input1 would be more consistent.
Although calling the second one output might be more
accurate?
> for line in input.readlines():
> line = re.sub('[\s]+', '', line)
you can just use the string replace method which for simple
replacements is faster than re and much less complex.
line = line.replace(' ','')
> y = line
> x = "\n"
> z = y + x
you don't really need all that
> input2.write(z)
input2.write(line + '\n')
is just as clear if not clearer! At least I think so! :-)
> del input
> del input2
You should use close on a file not del.
input1.close()
input2.close()
HTH,
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list