【问题标题】:TypeError: '>=' not supported between instances of 'float' and 'bytes'TypeError: 'float' 和 'bytes' 的实例之间不支持 '>='
【发布时间】:2020-09-10 09:40:40
【问题描述】:

谁能帮我解决我的这个问题?这是我的代码:

import RPi.GPIO as GPIO 
import time
import Adafruit_DHT
import urllib.request

GPIO.setmode (GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup (13, GPIO.OUT)
GPIO.output(13, 1)


def getSensorData(): 
   humidity, temp = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 22) 
   return (float(humidity), float(temp))

baseURL = 'https://mekatronika15.000webhostapp.com/data.php?api_key=%s'
inputURL= 'https://mekatronika15.000webhostapp.com/admin/tes2.php'
try:

   while True:
      humidity, temp = getSensorData()
      humidity = '%.2f' % humidity
      temp = '%.2f' % temp

      try:
         conn = urllib.request.urlopen(baseURL + "&humidity=%s&temp=%s" % (humidity, temp))
         conn1 = urllib.request.urlopen(inputURL)
         print (conn.read())
         conn.close()
         status1 = conn1.read()
         sleep(20)
      except: 
         print ('exiting.')
        
         print (humidity, temp)

    
      if float(temp) >= status1:
         GPIO.output(13, 0)
            
      elif float(temp) <= status1:
         GPIO.output(13, 1)    
         

except KeyboardInterrupt:
    GPIO.cleanup()

以及它给出的错误:

if float(temp) >= (status1):
TypeError: '>=' not supported between instances of 'float' and 'bytes'

不幸的是,我对python不熟悉所以我被卡住了,我知道这个应用程序中有很多类似的问题和答案,我尝试了一些但我仍然得到错误。

【问题讨论】:

    标签: python runtime-error typeerror


    【解决方案1】:

    由于您不需要在使用变量之前声明它,因此在尝试对变量进行操作时需要格外小心。

    问题是这一行status1 = conn1.read() 这里conn1.read()返回网页的内容,即源html作为字节序列(类似于字符串)。将数字与字符序列进行比较是未定义的操作;因此,python 引发了错误。

    您可以设计一个解析器来检索您需要的信息。例如,您可以使用encode() 将您拥有的字节转换为字符串。然后find() 你想要的status1 的索引。然后可以使用 substring 获取status1 为字符串,最后转换为数字。

    【讨论】:

    • “您可以设计一个解析器来检索您需要的信息”,在这种情况下,OP 使用预构建的可能会更容易解析器,例如标准库中的解析器,或 BeautifulSoup。
    • @ChristianDean 好样的!我不知道 python 有一个内置的解析器。谢谢!
    • 很高兴我能帮上忙! Python以随附电池而闻名。然而,在这方面,我听说 BeautifulSoup 是两者中的佼佼者。
    • 谢谢你的回答,我试试看!
    【解决方案2】:

    status1 以字节形式返回,需要进行格式化。

    print(status1) 在睡眠之前或使用调试器检查值。我的猜测是,您需要对收到的响应进行更多解包。

    requests 是 urllib 的包装器,通常更实用,因此您不需要所有这些步骤。

        response = requests.get(url)
        response.json  # probably has what you need
    

    【讨论】:

    • 谢谢你的回答,我试试看!
    【解决方案3】:

    urllib.request.urlopen() 函数返回一个HTTPResponse 对象:

    import urllib.request
    connection = urllib.request.urlopen("https://stackoverflow.com/")
    type(connection) # <class 'http.client.HTTPResponse'>
    

    The read method forHTTPConnection 对象(您使用语句 status1 = conn1.read() 调用),在文档中描述如下:

    读取并返回响应正文,或最多 下一个字节

    (强调我的)

    因此,您收到类型错误的原因是 HTTPConnection.read 返回 bytes,无法与 float 进行比较。您需要将bytes 转换为float。如果conn1.read() 只是返回一个字节形式的数字,你可以使用float(status1),但我高度怀疑status1 的格式有点复杂,所以你需要做一些挖掘来弄清楚到底是什么以及您希望如何从中提取数据。

    您可能希望查看像 BeautifulSoup 这样的 HTML 解析器,以帮助您提取所需的数据。

    【讨论】:

    • 感谢您的回答,我会注意您发送的链接!
    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 2020-05-05
    • 2021-03-18
    • 2019-11-12
    • 2018-05-19
    • 1970-01-01
    • 2022-09-23
    • 2021-05-20
    相关资源
    最近更新 更多