2017-01-28 85 views
0

我想通過tweepy學習Python。簡單的機器人非常簡單。測試字段是否與tweepy無

我創建了一個搜索單詞,但我想知道推文是從哪裏來的。

作爲一個測試,我有這段代碼:

if ' trump ' in status.text.lower(): 
    print status.coordinates 
    print ('geo'), status.geo 
    print status.text 

但我看到的座標與地理都是

如何測試以查看值是否爲零,並從看到這些結果中消除?

+2

您可以測試,看看是否值沒有,錯誤,如果值是None:'。 – jonrsharpe

+0

我添加了以下內容: if status.coordinates!=「無」 print status.coordinates但它顯示語法錯誤。我試過'和',但都不行,任何建議? – user6230466

+0

你錯過了一個冒號,你也應該使用身份('is' /'不是')不等於('=='/' !'=')與'None'進行比較,因爲它是一個singleton。 – jonrsharpe

回答

0

我可以建議你兩種可能的方式:

  1. 你可以在你的if語句添加一個條件如下:

    if 'trump' in status.text.lower() and status.coordinates is not None: 
    

    無不是一個字符串,那麼這是因爲原因,你的代碼(if status.coordinates != "None")引發了一個SyntaxError。

  2. 不要在if條件中更改或添加任何內容,並且只有當它不是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 
    
+0

現在我需要清理整個「File」again2.py「,第24行,在on_status writer.writerow([status.geo,status.coordinates,status.text]) UnicodeEncodeError:'ascii'編碼解碼器無法在位置118編碼字符u'\ u2026':序號不在範圍(128)「問題中 – user6230466

+0

很高興爲您提供幫助!無論如何,我不太瞭解您評論中的錯誤,我建議您打開另一個更詳細的問題。如果我的答案解決了您的第一個問題,請將其標記出來,以便對其他人有用用戶有同樣的問題,謝謝 – Giordano