如何检查变量是否存在然后在 python 中打印为字符串
How to check if variable exist then print as string in python
使用 GAE 的数据存储,我希望打印出一个语句,该语句与转换为字符串的变量连接。
这是我的代码片段,我循环遍历了 Article
类型的所有实体:
que = Article.query()
testt = que.fetch(1000)
for t in testt:
self.response.write(t.title)
self.response.write("<b>Artikel:</b> "+t.title + " <b>Forfatter:</b> "+t.author + " <b>Udgivet:</b> "
+ t.time + " <b>Likes:</b> " + str(t.likes) + " <b>Shares:</b> " + str(t.shares) + " <b>Comments:</b> " + str(t.comments))
然而,其中一些变量可能不存在。我猜这个错误是因为我正在尝试转换空值?
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~tipcrawl/1.383603861670919963/main.py", line 134, in get
+ t.time + " <b>Likes:</b> " + str(t.likes) + " <b>Shares:</b> " + str(t.shares) + " <b>Comments:</b> " + str(t.comments))
TypeError: coercing to Unicode: need string or buffer, NoneType found
所以我的问题是如何调用 if t.likes
语句来检查变量是否有值并连接在同一行上?
如果我是你,我会将输出存储在字符串 var 中,并在 var 存在时附加所需的内容:
testt = que.fetch(1000)
for t in testt:
self.response.write(t.title)
textToWrite = str()
if t.title:
textToWrite += "<b>Artikel:</b> "+ t.title
if t.author:
textToWrite += " <b>Forfatter:</b> "+t.author
# ....
# and other vars
#finally, write it
self.response.write(textToWrite)
希望对您有所帮助!
使用 GAE 的数据存储,我希望打印出一个语句,该语句与转换为字符串的变量连接。
这是我的代码片段,我循环遍历了 Article
类型的所有实体:
que = Article.query()
testt = que.fetch(1000)
for t in testt:
self.response.write(t.title)
self.response.write("<b>Artikel:</b> "+t.title + " <b>Forfatter:</b> "+t.author + " <b>Udgivet:</b> "
+ t.time + " <b>Likes:</b> " + str(t.likes) + " <b>Shares:</b> " + str(t.shares) + " <b>Comments:</b> " + str(t.comments))
然而,其中一些变量可能不存在。我猜这个错误是因为我正在尝试转换空值?
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~tipcrawl/1.383603861670919963/main.py", line 134, in get
+ t.time + " <b>Likes:</b> " + str(t.likes) + " <b>Shares:</b> " + str(t.shares) + " <b>Comments:</b> " + str(t.comments))
TypeError: coercing to Unicode: need string or buffer, NoneType found
所以我的问题是如何调用 if t.likes
语句来检查变量是否有值并连接在同一行上?
如果我是你,我会将输出存储在字符串 var 中,并在 var 存在时附加所需的内容:
testt = que.fetch(1000)
for t in testt:
self.response.write(t.title)
textToWrite = str()
if t.title:
textToWrite += "<b>Artikel:</b> "+ t.title
if t.author:
textToWrite += " <b>Forfatter:</b> "+t.author
# ....
# and other vars
#finally, write it
self.response.write(textToWrite)
希望对您有所帮助!