2017-05-19 30 views
0

我有一個LED標誌,我也發送天氣數據,但遇到溫度問題,讀取小數點,我相信這個標誌不會讀取,我得到這個錯誤。將數據點轉換爲整數蟒蛇

File "/usr/local/lib/python3.4/dist-packages/pyledsign/minisign.py", line 276, in processtags 
data=data.replace('<f:normal>',str(normal,'latin-1')) 
AttributeError: 'float' object has no attribute 'replace' 

這是我的代碼下面的標誌。發送此標記時會出現此錯誤。

mysign.queuemsg(data=current_weather.temperature, speed=2). 

所以我想知道我怎麼能說天氣溫度總是讀爲int。將int()放在它周圍不起作用。

#!/usr/bin/python 
import datetime 
import forecastio 
from pyledsign.minisign import MiniSign 

def main(): 
    """ 
    Run load_forecast() with the given lat, lng, and time arguments. 
    """ 

    api_key = 'my api key' 

    lat = 42.3314 
    lng = -83.0458 

    forecast = forecastio.load_forecast(api_key, lat, lng,) 

    mysign = MiniSign(devicetype='sign') 

    print ("===========Currently Data=========") 
    current_weather = forecast.currently() 
    print (current_weather.summary) 
    print (current_weather.temperature) 
    mysign.queuemsg(data=current_weather.summary, speed=2) 
    mysign.queuemsg(data=current_weather.temperature, speed=2) 
    mysign.sendqueue(device='/dev/ttyUSB0') 

    print ("===========Daily Data=========") 
    by_day = forecast.daily() 
    print ("Daily Summary: %s" %(by_day.summary)) 
    mysign.queuemsg(data=by_day.summary) 
    mysign.sendqueue(device='/dev/ttyUSB0') 

if __name__ == "__main__": 
    main() 
+0

它需要一個字符串,而不是浮動或整數 –

回答

0

錯誤來自期待字符串的方法。

在將數字發送到符號前,您應該明確地將您的數值轉換爲字符串。

mysign.queuemsg(data=str(current_weather.temperature), speed=2) 

你也可以做一些額外的字符串操作,如concatination。

mysign.queuemsg(data=str(current_weather.temperature) + ' degrees', speed=2) 

您還可以使用string formatting將該值轉換爲字符串並控制諸如小數位數等內容。要知道,字符串格式化不同在Python 2和Python 3。例如,在Python 2,你可以

mysign.queuemsg(data='%s degrees' % current_weather.temperature, speed=2) 
+0

工作完美!謝謝!我不認爲一個str會在之前工作,因爲來自另一個命令它會讀取實際的字'實例數據塊:' – N3VO

+0

@ N3VO太棒了!不要忘記投票並正確標記問題。 – Soviut

+0

實際上我還有一個問題,如果我想在一條消息中一起運行它們,那可能嗎?當我嘗試這個mysign.queuemsg(data = current_weather.summary,str(current_weather.temperature), speed = 2)時,我得到:'關鍵字arg後的非關鍵字arg' – N3VO