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 precisi
>> 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 w
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?
_
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 --
> -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
>
"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
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 %
"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 int
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.