开发者

weird python print behaviour

why this does not print anything:

for item in pipe开发者_如何学运维.json["value"]["items"]:
    print item["pubDate"]
but this does:
for item in pipe.json["value"]["items"]:
   print item["pubDate"] + "\n"
p.s. the loop is running inside another loop.

p.p.s. this is running inside google app engine application.i have looked at http response and it is completely empty in the first case.


It might be a problem with buffering, in which case flushing stdout would help.

import sys
sys.stdout.flush()


Are you using some sort of wsgi framework, or just trying to write pure CGI code (which is a mistake?)

You probably don't want to be using print at all here, but rather using your framework's method of adding to the response (for webapp, self.response.out.write). My guess would be that without the extra \n, you're writing all of this data to the HTTP headers, and with it you're only losing the first line of your output.


On GAE if you want to use print for output you'll have to print an empty string before any printing so that these kind of problems won't happen:

print ""
print "something"


This is just a wild guess, but if item['pubDate'] is a non-string object it might a result of differences between special methods. Perhaps the __str__ method returns nothing, while the __add__ method does something different.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜