我正在尝试通过tweepy学习Python。简单的机器人非常简单。
我创建了一个搜索单词,但我想知道推文来自哪个城市。
作为测试,我有一些代码:
if ' trump ' in status.text.lower():
print status.coordinates
print ('geo'), status.geo
print status.text
但是我看到坐标和地理位置都是无。
我如何测试以查看值是否为none,并且看不到这些结果?
答案 0 :(得分:0)
我可以建议你两种可能的方式:
您可以在if语句中添加条件,如下所示:
if 'trump' in status.text.lower() and status.coordinates is not None:
None不是字符串,因此这是因为您的代码(if status.coordinates != "None"
)引发了SyntaxError。
如果条件不要更改或添加任何内容,只有当它不是None时才打印该值。
可能的方法如下:
if status.coordinates is not None:
print status.coordinates
或
if status.coordinates:
print status.coordinates
和完整的脚本:
if 'trump' in status.text.lower():
if status.coordinates is not None:
print status.coordinates
if status.geo is not None:
print ('geo'), status.geo
print status.text