Just follow your prints or stdout.write()s with calls to
stdout.flush() to flush the buffer.
You could alternatively use the -u option to the python interpreter to
make the standard streams operate unbuffered.
Cheers,
Chris
--
http://blog.rebertia.com
http://docs.python.org/3.1/library/functions.html#print
a suitable object passed that makes I/O behave as if unbuffered would
be handy, if you don't want to stdout.flush() after a print()
You're presumably testing this in the interpreter, which prints extra
stuff. In particular, it prints the result value of any expressions
entered at the interpreter prompt. So if you type
sys.stdout.write("hello")
then after the write() method is done, the return value of the method
(5) will get printed by the interpreter.
Either put the statement in a real script, or do the following trick to
convince yourself:
dummy = sys.stdout.write("hello")
DaveA
Except sys.stdout.write("hello") doesn't return 5. It returns
None.
I don't know what the OP is talking about when he says "prints
the number of characters":
$ python
Python 2.6.2 (r262:71600, Aug 25 2009, 22:35:31)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import sys
>>> sys.stdout.write("hello\n")
hello
>>>
>>>
> Either put the statement in a real script, or do the following trick to
> convince yourself:
>
> dummy = sys.stdout.write("hello")
I don't see why the assignment is needed.
--
Grant
Presumably he's using Python 3:
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
p3> import sys
p3> sys.stdout.write("hello")
hello5
See http://bugs.python.org/issue6345
--
Gabriel Genellina
> On Fri, 16 Oct 2009 23:39:38 -0400, Dave Angel <da...@ieee.org>
> declaimed the following in gmane.comp.python.general:
>
>
>> You're presumably testing this in the interpreter, which prints extra
>> stuff. In particular, it prints the result value of any expressions
>> entered at the interpreter prompt. So if you type
>>
>> sys.stdout.write("hello")
>>
>> then after the write() method is done, the return value of the method
>> (5) will get printed by the interpreter.
>>
> I was about to respond that way myself, but before doing so I
wanted
> to produce an example in the interpreter window... But no effect?
>
> C:\Documents and Settings\Dennis Lee Bieber>python ActivePython 2.5.2.2
> (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, Mar 27
> 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> sys.stdout.write("hello")
> hello>>>
>
>
> PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit
> (Intel)] on win32.
> Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin'
> for further copyright information.
>>>> import sys
>>>> sys.stdout.write("This is a test")
> This is a test
>>>>
>>>> print sys.stdout.write("Hello")
> HelloNone
>>>>
>>>>
>
> No count shows up... neither PythonWin or Windows command line/
shell
Indeed I'm using py3. But now everythong is fine. Everything I just
wanted to know was just to run this simple script (I've also sent the msg
'putchar(8)' to the newsgroup):
import time
import sys
val = ("|", "/", "-", "\\", "|", "/", "-", "\\")
for i in range(100+1):
print("=", end="")
# print("| ", end="")
print(val[i%len(val)], " ", sep="", end="")
print(i, "%", sep="", end="")
sys.stdout.flush()
time.sleep(0.1)
if i > 9:
print("\x08"*5, " "*5, "\x08"*5, sep="", end="")
else:
print("\x08"*4, " "*4, "\x08"*4, sep="", end="")
print(" 100%\nDownload complete!")
Another question (always py3). How can I print only the first number
after the comma of a division?
e.g. print(8/3) --> 2.66666666667
I just want 2.6 (or 2.66)
Thanks, Mattia
x = 8/3
dummy0=dummy1=dummy2=42
s = "The answer is approx. {3:07.2f} after rounding".format(dummy0,
dummy1, dummy2, x)
print(s)
will print out the following:
The answer is approx. 0002.67 after rounding
A brief explanation of the format string {3:07.2f} is as follows:
3 selects argument 3 of the function, which is x
0 means to zero-fill the value after conversion
7 means 7 characters total width (this helps determine who many
zeroes are inserted)
2 means 2 digits after the decimal
f means fixed point format
You can generally leave out the parts you don't need, but this gives you
lots of control over what things should look like. There are lots of
other parts, but this is most of what you might need for controlled
printing of floats.
The only difference from what you asked is that this rounds, where you
seemed (!) to be asking for truncation of the extra columns. If you
really need to truncate, I'd recommend using str() to get a string, then
use index() to locate the decimal separator, and then slice it yourself.
DaveA
Consider:
import time, sys, itertools
val = ("|", "/", "-", "\\", "|", "/", "-", "\\")
sys.stdout.write(" ")
pattern = "\x08"*8 + " {0}{1:02d}%"
for percentage, string in enumerate(itertools.cycle(val)):
if percentage>99 : break
paddednum = pattern.format(string, percentage)
sys.stdout.write(paddednum)
sys.stdout.flush()
time.sleep(0.1)
print("\x08\x08\x08\x08 100%\nDownload complete!")
Note the use of cycle() which effectively repeats a list indefinitely.
And enumerate, which makes an index for you automatically when you're
iterating through a list. And str.format() that builds our string,
including using 0 padding so the percentages are always two digits.
DaveA
Are you sure you don't want that to be 2.7 or 2.67? Then you can use:
n = int(n * 10**2) / 10**2
else if 2.7 pr 2.67 is what you wanted, you could use:
n = round(n, 2)
Best to use format(), the way it was intended. Round to decimal while
converting to decimal. Otherwise surprises await in dark corners.
DaveA
> Presumably he's using Python 3:
And apparently not IDLE
> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> p3> import sys
> p3> sys.stdout.write("hello")
> hello5
>
> See http://bugs.python.org/issue6345
IDLE
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
>>> import sys
>>> sys.stdout.write('abc')
abc
reported, for better or worse, in http://bugs.python.org/issue7163
Since interactive users do not usually use sys.stdout.write (versus
print), the mixed output is not usually a problem.
tjr
Apparently, Pythonwin has the same "problem":
PythonWin 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for
further copyright information.
>>> import sys
>>> sys.stdout.write('hello')
hello
>>> print(sys.stdout.write('hello'))
helloNone
-Mark
It's always good to learn something new, thanks!
Yes, reading the doc I've come up with
s = "%(0)03.02f%(1)s done" % {"0": 100.0-100.0*(size/tot), "1": "%"}
but to it is not a good idea to use a dict here..
Also look at the new str.format()
> Il Sat, 17 Oct 2009 10:02:27 -0400, Dave Angel ha scritto:
>> mattia wrote:
>>> Il Fri, 16 Oct 2009 21:04:08 +0000, mattia ha scritto:
>>>
>>> Another question (always py3). How can I print only the first number
>>> after the comma of a division?
>>> e.g. print(8/3) --> 2.66666666667
>>> I just want 2.6 (or 2.66)
>>>
>> x = 8/3
>> dummy0=dummy1=dummy2=42
>> s = "The answer is approx. {3:07.2f} after rounding".format(dummy0,
>> dummy1, dummy2, x)
>> print(s)
>>
>> will print out the following:
>>
>> The answer is approx. 0002.67 after rounding
>
> Yes, reading the doc I've come up with
> s = "%(0)03.02f%(1)s done" % {"0": 100.0-100.0*(size/tot), "1": "%"}
> but to it is not a good idea to use a dict here..
No need for a dict, you could use instead:
s = "%03.02f%s done" % (100.0-100.0*(size/tot), "%")
or (%% is the way to embed a single %):
s = "%03.02f%% done" % (100.0-100.0*(size/tot),)
or even:
s = "%03.02f%% done" % (100.0-100.0*(size/tot))
but the new str.format() originally suggested by Dave Angel is better:
s = "{0:03.02f}% done".format(100.0-100.0*(size/tot))
(BTW, why 03.02f? The output will always have at least 4 chars, so 03
doesn't mean anything... Maybe you want {0:06.2f} (three places before the
decimal point, two after it, filled with 0's on the left)?)
--
Gabriel Genellina
No need of 03, you are right, thanks.
DaveA
+1 QOTW