[Tutor] search-replace
Hi tutors If I need to clean a textfile (perhaps after web scraping), I have used this method - without problems - but I'm sure there must be smarter and better ways. I'm especially interested to know how I do more than just one search-replace without having to repeat the whole step below. I'm using Python 2.6.6 on a windows pc. fin = open("dirtyfile.txt") fout = open("cleanfile.txt", "w") for line in fin: fout.write(line.replace('## ', '#')) fin.close() fout.close() Thanks, Tommy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] about print()
"Ryan Wu" wrote I am a newbie of python, and reading 'python essential reference'. Now I want to print this results 'a is %d' % a -> a is 42 with the code a = 42 test = "'a is %d' % a" test is now a literal string print( '%20s ->' % test, test) And this inserts the literal string into the format string then prints the literal string What I think you wanted to do is: a = 42 test = 'a is %d' print( '%20s -> %s' % (test, test % a) ) What is the difference of print(test) and print ( 'a is %d' % a )? In test you have the a inside the string delimiters so it is part of the string. In the second case a is outside the string and gets evaluated as a variable HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] about print()
Oh, I see! It's a little stupid question :) Thanks,Alan! On 6/6/11, Alan Gauld wrote: > "Ryan Wu" wrote > >> I am a newbie of python, and reading 'python essential reference'. >> >> Now I want to print this results >> 'a is %d' % a -> a is 42 >> >> with the code >> >> a = 42 >>> test = "'a is %d' % a" > > test is now a literal string > >>> print( '%20s ->' % test, test) > > And this inserts the literal string into the format string then > prints the literal string > > What I think you wanted to do is: > a = 42 test = 'a is %d' > print( '%20s -> %s' % (test, test % a) ) > >> What is the difference of print(test) and print ( 'a is %d' % a )? > > In test you have the a inside the string delimiters so it is part > of the string. In the second case a is outside the string and > gets evaluated as a variable > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Ryan Wu ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search-replace
"Tommy Kaas" wrote I'm especially interested to know how I do more than just one search-replace without having to repeat the whole step below. fin = open("dirtyfile.txt") fout = open("cleanfile.txt", "w") for line in fin: fout.write(line.replace('## ', '#')) fin.close() fout.close() Can be simplified to: with open("cleanfile.txt", "w") as fout: for line in open("dirtyfile.txt"): fout.write(line.replace('## ', '#')) To do multiple replaces simply expand the inner block for line in open("dirtyfile.txt"): line = line.replace() #first line = line.replace() #second etc fout.write(line) Or if you have a lot of them: replacePatterns = [('##','#'),('!!!','!!'),] for line in open("dirtyfile.txt"): for old,new in repace_patterns: line = line.replace(old,new) fout.write(line) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search-replace
> -Oprindelig meddelelse- > Fra: tutor-bounces+tommy.kaas=kaasogmulvad...@python.org > [mailto:tutor-bounces+tommy.kaas=kaasogmulvad...@python.org] På vegne > af Alan Gauld > Sendt: 6. juni 2011 11:51 > Til: tutor@python.org > Emne: Re: [Tutor] search-replace > > > "Tommy Kaas" wrote > > > > I'm especially interested to know how I do more than just one > > search-replace without having to repeat the whole step below. > > fin = open("dirtyfile.txt") > > fout = open("cleanfile.txt", "w") > > for line in fin: > >fout.write(line.replace('## ', '#')) > > fin.close() > > fout.close() > > Can be simplified to: > > with open("cleanfile.txt", "w") as fout: > for line in open("dirtyfile.txt"): >fout.write(line.replace('## ', '#')) > > To do multiple replaces simply expand the inner block > > > for line in open("dirtyfile.txt"): >line = line.replace() #first >line = line.replace() #second etc >fout.write(line) > > Or if you have a lot of them: > > replacePatterns = [('##','#'),('!!!','!!'),] > > for line in open("dirtyfile.txt"): > for old,new in repace_patterns: > line = line.replace(old,new) > fout.write(line) > Excellent! Thanks. tommy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] search-replace
Or, open the file as a blob (one long string) and do a single 'replace'. fin = open("dirtyfile.txt", 'r').read().replace('## ', '#') open("dirtyfile.txt", 'w').write(fin) or, open("dirtyfile.txt", 'w').write(open("dirtyfile.txt", 'r').read().replace('## ', '#')) - Original Message - From: "Tommy Kaas" To: Sent: Monday, June 06, 2011 2:58 AM Subject: Re: [Tutor] search-replace -Oprindelig meddelelse- Fra: tutor-bounces+tommy.kaas=kaasogmulvad...@python.org [mailto:tutor-bounces+tommy.kaas=kaasogmulvad...@python.org] På vegne af Alan Gauld Sendt: 6. juni 2011 11:51 Til: tutor@python.org Emne: Re: [Tutor] search-replace "Tommy Kaas" wrote > I'm especially interested to know how I do more than just one > search-replace without having to repeat the whole step below. > fin = open("dirtyfile.txt") > fout = open("cleanfile.txt", "w") > for line in fin: >fout.write(line.replace('## ', '#')) > fin.close() > fout.close() Can be simplified to: with open("cleanfile.txt", "w") as fout: for line in open("dirtyfile.txt"): fout.write(line.replace('## ', '#')) To do multiple replaces simply expand the inner block for line in open("dirtyfile.txt"): line = line.replace() #first line = line.replace() #second etc fout.write(line) Or if you have a lot of them: replacePatterns = [('##','#'),('!!!','!!'),] for line in open("dirtyfile.txt"): for old,new in repace_patterns: line = line.replace(old,new) fout.write(line) Excellent! Thanks. tommy ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] floats
i saw it somewhere, but where? i want to 10 / 1000 and get 0.01 not 0 if 1000 is made 1000.00 then 0.01 is printed but that gives 500 / 1000.00 is 0.5 not 0.50 i might be thinking C# not python. can someone till me how to get a two decimal precision every time? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] floats
>> Can someone till me how to get a two decimal precision every time? print "%.2f" % (500/1000.0) # or... result = 500 / 1000.0 print "%.2f" % result Using 'new' style string formatting works too: print "{0:.2f}".format(500/1000.0) -Modulok- On 6/6/11, Michael bridges wrote: > i saw it somewhere, but where? > > i want to 10 / 1000 and get 0.01 not 0 > if 1000 is made 1000.00 then 0.01 is printed > but that gives 500 / 1000.00 is 0.5 not 0.50 > > i might be thinking C# not python. > > can someone till me how to get a two decimal precision every time? > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] floats
On 2011/06/07 04:43 AM, Michael bridges wrote: i saw it somewhere, but where? i want to 10 / 1000 and get 0.01 not 0 if 1000 is made 1000.00 then 0.01 is printed but that gives 500 / 1000.00 is 0.5 not 0.50 i might be thinking C# not python. can someone till me how to get a two decimal precision every time? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor >>> 100/1000 0 >>> from __future__ import division >>> 100/1000 0.1 >>> 100//1000 0 So you can still get the old behaving floor division using double divisors and any normal syntax will be true division. In Python 3.x it's already the standard, this is only necessary for Python 2.x -- Christian Witts // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor