2017-05-19 29 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