This is a difficult issue to search for, and Googling (and reviewing
the pertinent Python docs) has not found me anything useful. It's also
not a super important issue, but regardless of whether it's avoidable,
I still want to know what the heck is going on.
Doing multiple print statements inside a for loop, using the 'comma at
the end eats the newline' variant, results in zero output until the
loop completes its entire iteration.
def test():
print "testing:",
for x in range(10):
time.sleep(1)
print ".",
print "done."
Executing test() results in the behavior described above. If one
removes the commas after the first two print statements, it behaves
normally, with "testing" showing up right away, then a period on each
newline every second until the end.
I've tried various things--os.system('sleep 1') instead of
time.sleep(1), sys.stdout.write() instead of print-with-a-comma, and
other equally silly tactics, to no avail.
What's going on here?
Thanks,
Jeff 3 1305
Jeffrey E. Forcier wrote: Doing multiple print statements inside a for loop, using the 'comma at the end eats the newline' variant, results in zero output until the loop completes its entire iteration.
sys.stdout is line buffered. Put an intervening sys.stdout.flush() in
between your prints.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Substance is one of the greatest of our illusions.
-- Sir Arthur Eddington
Take a look at this recipe which is part of the latest
Python Cookbook. I think it will help you. http://aspn.activestate.com/ASPN/Coo.../Recipe/299207
Larry Bates
Jeffrey E. Forcier wrote: This is a difficult issue to search for, and Googling (and reviewing the pertinent Python docs) has not found me anything useful. It's also not a super important issue, but regardless of whether it's avoidable, I still want to know what the heck is going on.
Doing multiple print statements inside a for loop, using the 'comma at the end eats the newline' variant, results in zero output until the loop completes its entire iteration.
def test(): print "testing:", for x in range(10): time.sleep(1) print ".", print "done."
Executing test() results in the behavior described above. If one removes the commas after the first two print statements, it behaves normally, with "testing" showing up right away, then a period on each newline every second until the end.
I've tried various things--os.system('sleep 1') instead of time.sleep(1), sys.stdout.write() instead of print-with-a-comma, and other equally silly tactics, to no avail.
What's going on here?
Thanks, Jeff
Erik: Thanks, that was it! Figured it was something along those lines,
but like I said, I didn't know specifically what was going on or what
to look for.
Larry: That recipe could be useful, thanks a lot :)
And thanks for the quick replies, too, you guys are awesome.
Regards,
Jeff This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sathyashrayan |
last post by:
Dear group,
Following is a exercise from a book called "Oreilly's
practical C programming". I just wanted to do a couple of C
programming exercise. I do have K and R book, but let me try
some...
|
by: Yahooooooooo |
last post by:
no gotos
no loops
how is it possbile.....
Asked in interview ...
Regards
MA
|
by: Neo Geshel |
last post by:
Greetings.
BACKGROUND:
My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are
delivered as application/xhtml+xml for all non-MS web clients, and as
text/xml for all MS web...
|
by: -Lost |
last post by:
For example:
var newlines = 'a\n\nb\n\nc';
alert(newlines);
Yet, if I get that *exact* same line from an XMLHttpRequest's
responseText, it is always alerted as:
a\n\nb\n\nc
|
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= |
last post by:
Is there a way to force printing in black and white programatically?
|
by: lihao0129 |
last post by:
Hi, folks:
I recently went through a strange problem with my Javascript code,
say: I have a string variable which are from a 'textarea' element and
I want to remove the trailing newlines inside...
|
by: joemacbusiness |
last post by:
Hi All,
How do I format printed data in python?
I could not find this in the Python Reference Manual:
http://docs.python.org/ref/print.html
Nor could I find it in Matloff's great tutorial:...
|
by: ertis6 |
last post by:
Hi all,
I need to calculate a value inside 8 nested for loops. 2 additional
for loops are used during calculation. It was working fine with 4
loops. My code is like this:
...
for(int i1=0;...
|
by: it0ny |
last post by:
Hi guys,
thanks I am fairly new to this forum so I hope I chose the right place to post this question.
I try to make my program printout a deposit's report. I created a class to store the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |