将数据点转换为整数 python

Converting datapoint to integer python

我有一个 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() 放在它周围是行不通的。

#!/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()

错误来自需要字符串的方法。

您应该在将数值发送到标志之前明确地将它们转换为字符串。

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

您还可以进行一些额外的字符串操作,例如连接。

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)