{"id":2724,"date":"2011-03-20T14:54:51","date_gmt":"2011-03-20T22:54:51","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=2724"},"modified":"2011-03-20T14:55:05","modified_gmt":"2011-03-20T22:55:05","slug":"learning-python-the-hard-way-print-vs-sys-write-and-python-u","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2011\/03\/20\/learning-python-the-hard-way-print-vs-sys-write-and-python-u\/","title":{"rendered":"Learning python the hard way: print vs sys.write, and python -u"},"content":{"rendered":"<p>I knew before that print in Python had some weird properties. Like: <\/p>\n<blockquote><p>\n<code><\/p>\n<p>>>> for i in [1, 2, 3, 4]:<br \/>\n...     print \"blah\"<br \/>\n...<br \/>\nblah<br \/>\nblah<br \/>\nblah<br \/>\nblah<br \/>\n>>> for i in [1, 2, 3, 4]:<br \/>\n...     print \"blah\",<br \/>\n...<br \/>\nblah blah blah blah<br \/>\n<\/code>\n<\/p><\/blockquote>\n<p>One thing you&#8217;ll notice is that there&#8217;s a space between each of the blahs. If you don&#8217;t want those spaces, you need to use sys.write. <a href=\"http:\/\/code.activestate.com\/recipes\/576986-progress-bar-for-console-programs-as-iterator\/\">Here&#8217;s an example of using sys.write<\/a> along with a progress bar indicator.  Which is exactly what I wanted this for. <\/p>\n<p>Finally, you can indicate to <a href=\"http:\/\/docs.python.org\/using\/cmdline.html#cmdoption-unittest-discover-u\">python on the command-line that you want unbuffered stdin and stdout<\/a> with <code>python -u<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I knew before that print in Python had some weird properties. Like: >>> for i in [1, 2, 3, 4]: &#8230; print &#8220;blah&#8221; &#8230; blah blah blah blah >>> for i in [1, 2, 3, 4]: &#8230; print &#8220;blah&#8221;, &#8230; &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2011\/03\/20\/learning-python-the-hard-way-print-vs-sys-write-and-python-u\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[431,660,433,432],"class_list":["post-2724","post","type-post","status-publish","format-standard","hentry","category-python","tag-print","tag-python","tag-python-u","tag-sys-write"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/comments?post=2724"}],"version-history":[{"count":2,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2724\/revisions"}],"predecessor-version":[{"id":2747,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/2724\/revisions\/2747"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=2724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=2724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=2724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}