Re: [Tutor] Space the final frontier
> 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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier!
Hi John, what do you think about this? def InplaceReplace(filename): lines = open(filename, 'r').readlines() lines = [line.replace(' ', '') for line in lines] open(filename, 'wt').writelines(lines) It works well on so small files. Yours sincerely, __ János Juhász > Message: 2 > Date: Tue, 4 Apr 2006 22:33:18 +0100 > From: "John Corry" <[EMAIL PROTECTED]> > Subject: [Tutor] Space the final frontier! > To: > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > Dear All, > I am having difficulty removing white spaces from my file. The file is 999 > lines long and looks like the sample below: > 001, new field,dial= 028 90 79 0154, dial= > 002, borfiled, dial= 02890 618521, dial= > 003, newcomp, dial=02890419689, dial= > The program, I am using to import the file does not like the spaces around > the numbers. The number should look like the "dial=02890419689" in the > third line. Thus the sample above should look like: > 001,newfield,dial=02890790154,dial= > 002,borfiled,dial=02890618521,dial= > 003,newcomp,dial=02890419689,dial= ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier
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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Space the final frontier
Dear All, Thanks for the prompt replies. I have now processed my file with 999 lines and it took seconds. It was beautiful. 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. filename = "a:/calllist.csv" filename2 = "c:/calllist.csv" import string import os import re listy = [] 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) del input del input2 Thanks again, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier!
Hi! > I am having difficulty removing white spaces from my file. The file is > 999 line = line.replace(' ','') should do the work Carlo. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier!
[Sorry for the initial misfire, John] > [mailto:[EMAIL PROTECTED] On Behalf Of John Corry > 001,newfield,dial=02890790154,dial= > 002,borfiled,dial=02890618521,dial= > 003,newcomp,dial=02890419689,dial= Hi John: I believe the common idiom in this case is ''.join( theString.split( ' ' ) ) >>> theLines = [ '001,newfield,dial=02890790154,dial=', '002,borfiled,dial=02890618521,dial=', '003,newcomp,dial=02890419689,dial='] >>> for line in theLines: print ''.join( line.split( ' ' ) ) 001,newfield,dial=02890790154,dial= 002,borfiled,dial=02890618521,dial= 003,newcomp,dial=02890419689,dial= Regards, Ryan --- Ryan Ginstrom http://ginstrom.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Space the final frontier!
Hi John, It would be easier to do all of your whitespace elimination before you append the string to your list(s). Something like this I should get you started: for line in input.readlines(): line = re.sub('[\s]+', '', line) listy.append(line) print listy bonus points for appending the return from re.sub to your list. :) -mtw On Tue, Apr 04, 2006 at 10:33:18PM +0100, John Corry ([EMAIL PROTECTED]) wrote: > Dear All, > > I am having difficulty removing white spaces from my file. The file is 999 > lines long and looks like the sample below: > > 001, new field,dial= 028 90 79 0154, dial= > 002, borfiled, dial= 02890 618521, dial= > 003, newcomp, dial=02890419689, dial= > > The program, I am using to import the file does not like the spaces around > the numbers. The number should look like the "dial=02890419689" in the > third line. Thus the sample above should look like: > > 001,newfield,dial=02890790154,dial= > 002,borfiled,dial=02890618521,dial= > 003,newcomp,dial=02890419689,dial= > > I have searched the tutor mailbag already and have picked up some good tips > on join, split and re but I can't seem to get it to work. > > I am using the following code. > > filename = "c:/test.txt" > import string > import os > import re > listy = [] > input = open( filename, 'r') #read access > for line in input.readlines(): > y = line > listy.append(y) > print listy > x = listy.pop() > > re.sub(r'\s', '', x) > print y,x > > del input > > It produces the output: > > ['001, new field,dial= 028 90 79 0154, dial=\n'] > 001, new field,dial= 028 90 79 0154, dial= > 001, new field,dial= 028 90 79 0154, dial= > > ['002, borfiled, dial= 02890 618521, dial=\n'] > 002, borfiled, dial= 02890 618521, dial= > 002, borfiled, dial= 02890 618521, dial= > > ['003, newcomp, dial=02890419689, dial='] > 003, newcomp, dial=02890419689, dial= 003, newcomp, dial=02890419689, dial= > > Any help would be greatly appreciated. > > Regards, > > John. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Matthew White - District Systems Administrator Tigard/Tualatin School District 503.431.4128 "The greatest thing in this world is not so much where we are, but in what direction we are moving." -Oliver Wendell Holmes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Space the final frontier!
Dear All, I am having difficulty removing white spaces from my file. The file is 999 lines long and looks like the sample below: 001, new field,dial= 028 90 79 0154, dial= 002, borfiled, dial= 02890 618521, dial= 003, newcomp, dial=02890419689, dial= The program, I am using to import the file does not like the spaces around the numbers. The number should look like the "dial=02890419689" in the third line. Thus the sample above should look like: 001,newfield,dial=02890790154,dial= 002,borfiled,dial=02890618521,dial= 003,newcomp,dial=02890419689,dial= I have searched the tutor mailbag already and have picked up some good tips on join, split and re but I can't seem to get it to work. I am using the following code. filename = "c:/test.txt" import string import os import re listy = [] input = open( filename, 'r')#read access for line in input.readlines(): y = line listy.append(y) print listy x = listy.pop() re.sub(r'\s', '', x) print y,x del input It produces the output: ['001, new field,dial= 028 90 79 0154, dial=\n'] 001, new field,dial= 028 90 79 0154, dial= 001, new field,dial= 028 90 79 0154, dial= ['002, borfiled, dial= 02890 618521, dial=\n'] 002, borfiled, dial= 02890 618521, dial= 002, borfiled, dial= 02890 618521, dial= ['003, newcomp, dial=02890419689, dial='] 003, newcomp, dial=02890419689, dial= 003, newcomp, dial=02890419689, dial= Any help would be greatly appreciated. Regards, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor