python TypeError:需要float参数,而不是lis

2024-09-24 04:26:40 发布

您现在位置:Python中文网/ 问答频道 /正文

非商业,非专业用户,为业余项目寻求建议。

我有两个Pi设置成这样: pi1托管一个Pimoroni Enviro Phat并将其读数写入pi1上的数据库

Pi2拥有一个皮莫罗尼卷轴图案。 我希望它从Pi1上的数据库中读取数据,并在Pi2上的滚动Phat HD上显示

我已经建立了所有必要的Mysql连接要求,它们都很好。在

如何从Pi1上的数据库中获取读数,然后在pi2上滚动PhatHD?在

import signal
import time
import scrollphathd
from scrollphathd.fonts import font5x7
import mysql.connector
from mysql.connector import Error
con = mysql.connector.connect(host='192.168.#.##',database='test',user='#####',password='#######')


cur = con.cursor() 
cur.execute('SELECT Yaxis FROM readings ORDER BY Added DESC LIMIT 1')


### Keep trailing comma from SELECT result away: 
result = [row[0] for row in cur.fetchall()]

### Show result of SELECT query in terminal
print result

# temperature = int((float('result')))
temperature = ['result']

# Write the "Hello World!" string in the buffer and
#   set a more eye-friendly default brightness
scrollphathd.write_string(" Hello World! %.1fC "%(temperature), brightness=0.5)

# Auto scroll using a while + time mechanism (no thread)
while True:
    # Show the buffer
    scrollphathd.show()
    # Scroll the buffer content
    scrollphathd.scroll()
    # Wait for 0.1s
    time.sleep(0.1)

错误读取:“TypeError:需要float参数,而不是list” 谢谢您。在


好的,我的这个很好用。Pi2正在从Pi1上的数据库中读取三个参数,并在某些if/elif中使用它们,以便在滚动条上适当地显示它们。在

我只是想知道。在Python中有没有一种方法可以将数据库连接信息保存在一个单独的文件中,就像我在PHP中使用require的方式一样? 谢谢您。在


Tags: theinfromimport数据库connectortimemysql
2条回答

排队

temperature = ['result']

temperature变量设置为列表,而

scrollphathd.write_string(" Hello World! %.1fC "%(temperature), brightness=0.5)

期望temperature变量是float类型的变量。在

temperature = result[0]替换该行可以解决这个问题,但是请注意,它不处理查询返回空响应的情况。在

您的错误似乎是由以下原因造成的:

scrollphathd.write_string(" Hello World! %.1fC "%(temperature), brightness=0.5)

您试图将带有temperature的字符串格式化为float,但就在上面您写了一个列表temperature = ['result']。在

相关问题 更多 >