python TypeError : float argument required, 未列出

标签 python mysql raspberry-pi

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

我有两个 Pi,如下所示: Pi 1 托管 Pimoroni Enviro Phat 并将其读数写入同样位于 Pi 1 上的数据库

Pi 2 托管 Pimoroni Scroll PhatHD。 我希望它从 Pi 1 上的数据库读取数据,并将其显示在 Pi 2 上的 Scroll Phat HD 上

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

如何从 Pi1 上的数据库获取读数以在 Pi 2 上的 Scroll 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:需要浮点参数,而不是列表” 谢谢。

<小时/>

好的,我的这个运行良好。 Pi2 正在从 Pi1 上的数据库中读取三个参数,并在某些 if/elif 中使用它们,以将它们适本地显示在 Scroll PhatHD 上。

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

最佳答案

线路

温度=['结果']

温度变量设置为列表,而行

scrollphathd.write_string("Hello World!%.1fC"%(温度),亮度=0.5)

期望温度变量是float类型变量。

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

关于python TypeError : float argument required, 未列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513852/

相关文章:

python - 解析电子邮件并从正文中获取号码

python - 不同轴的 numpy sum 的速度

python - 如何调整每周分析的移动平均线?

mysql - 在 MySQL 中连接具有相同 ID 的行的字段

c - 为什么在用 gcc 编译后一个值消失了?

raspberry-pi - 树莓派没有/etc/modprobe.d/raspi-blacklist.conf

python - Mac OS X 上的 wxPython 2.9

php - 单击导航栏中的选项时更改 $sql

mysql - 你能像在 SQL Server 中那样在 mySQL 中使用批处理 SQL 吗?

c# - UWP 应用程序中的 Async Task.Delay 开销,是否有更好的解决方案?