python报错对象没有属性_python-AttributeError:“ float”对象没有属性“...

我只是在学习Python,却不知道如何解决错误.下面的Python代码在Raspberry Pi 16×2屏幕上写一些消息.在第三组消息#AVG SPEED上,如果我使用print print avgspeed,它可以正常工作,但是当我将其输出到屏幕上时,出现此错误AttributeError:’float’对象没有属性’center’

import RPi.GPIO as GPIO

import time

from time import sleep

import socket

import fcntl

import struct

from pycgminer import CgminerAPI

LCD_RS = 25

LCD_E = 24

LCD_D4 = 23

LCD_D5 = 17

LCD_D6 = 18

LCD_D7 = 22

LCD_WIDTH = 16

LCD_CHR = True

LCD_CMD = False

LCD_LINE_1 = 0x80

LCD_LINE_2 = 0xC0

E_PULSE = 0.00005

E_DELAY = 0.00005

def main():

GPIO.setmode(GPIO.BCM)

GPIO.setup(LCD_E, GPIO.OUT)

GPIO.setup(LCD_RS, GPIO.OUT)

GPIO.setup(LCD_D4, GPIO.OUT)

GPIO.setup(LCD_D5, GPIO.OUT)

GPIO.setup(LCD_D6, GPIO.OUT)

GPIO.setup(LCD_D7, GPIO.OUT)

lcd_init()

#UNIT

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("UNIT 1",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string("MODEL BETA",2)

sleep(5)

#CLEAR

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string("",2)

sleep(0.4)

#IP ADDRESS

pi_ip = get_ip_address('eth0')

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("IP Address",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string(pi_ip,2)

sleep(5)

#CLEAR

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string("",2)

sleep(0.4)

# AVG SPEED

cgminer = CgminerAPI()

summary = cgminer.summary()

avgspeed = avg_speed()

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("Avg. Speed",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string(avgspeed,2)

sleep(5)

#CLEAR

lcd_byte(LCD_LINE_1, LCD_CMD)

lcd_string("",2)

lcd_byte(LCD_LINE_2, LCD_CMD)

lcd_string("",2)

sleep(0.4)

def get_ip_address(ifname):

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

return socket.inet_ntoa(fcntl.ioctl(

s.fileno(),

0x8915, # SIOCGIFADDR

struct.pack('256s', ifname[:15])

)[20:24])

def avg_speed():

cgminer = CgminerAPI()

summary = cgminer.summary()

avg_speed_value = cgminer.command('summary')['SUMMARY'][0]['MHS av']

return avg_speed_value

def lcd_init():

lcd_byte(0x33,LCD_CMD)

lcd_byte(0x32,LCD_CMD)

lcd_byte(0x28,LCD_CMD)

lcd_byte(0x0C,LCD_CMD)

lcd_byte(0x06,LCD_CMD)

lcd_byte(0x01,LCD_CMD)

def lcd_string(message,style):

if style==1:

message = message.ljust(LCD_WIDTH," ")

elif style==2:

message = message.center(LCD_WIDTH," ")

elif style==3:

message = message.rjust(LCD_WIDTH," ")

for i in range(LCD_WIDTH):

lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):

GPIO.output(LCD_RS, mode)

GPIO.output(LCD_D4, False)

GPIO.output(LCD_D5, False)

GPIO.output(LCD_D6, False)

GPIO.output(LCD_D7, False)

if bits&0x10==0x10:

GPIO.output(LCD_D4, True)

if bits&0x20==0x20:

GPIO.output(LCD_D5, True)

if bits&0x40==0x40:

GPIO.output(LCD_D6, True)

if bits&0x80==0x80:

GPIO.output(LCD_D7, True)

time.sleep(E_DELAY)

GPIO.output(LCD_E, True)

time.sleep(E_PULSE)

GPIO.output(LCD_E, False)

time.sleep(E_DELAY)

GPIO.output(LCD_D4, False)

GPIO.output(LCD_D5, False)

GPIO.output(LCD_D6, False)

GPIO.output(LCD_D7, False)

if bits&0x01==0x01:

GPIO.output(LCD_D4, True)

if bits&0x02==0x02:

GPIO.output(LCD_D5, True)

if bits&0x04==0x04:

GPIO.output(LCD_D6, True)

if bits&0x08==0x08:

GPIO.output(LCD_D7, True)

time.sleep(E_DELAY)

GPIO.output(LCD_E, True)

time.sleep(E_PULSE)

GPIO.output(LCD_E, False)

time.sleep(E_DELAY)

if __name__ == '__main__':

main()

追溯:

Traceback (most recent call last):

File "screen.py", line 165, in

main()

File "screen.py", line 81, in main

lcd_string(avgspeed,2)

File "screen.py", line 117, in lcd_string

message = message.center(LCD_WIDTH," ")

AttributeError: 'float' object has no attribute 'center'

有人可以帮我吗?

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为numpy.float64对象没有fillna属性。要解决这个问题,您需要首先确保您的对象是pandas的DataFrame或Series类型,因为fillna()方法是这些类型的方法之一。您可以使用pandas的astype()方法将您的对象转换为DataFrame或Series类型,然后再尝试使用fillna()方法。请参考下面的代码示例: ```python import pandas as pd # 转换为DataFrame或Series类型 your_object = your_object.astype(pd.DataFrame) # 或者 pd.Series # 使用fillna()方法 your_object = your_object.fillna(value) ``` 在上面的代码中,您需要将"your_object"替换为您实际使用的对象,并将"value"替换为您要用来填充缺失值的值。 这样,您就可以解决AttributeError: 'numpy.float64' object has no attribute 'fillna'的问题了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [loss训练记录](https://blog.csdn.net/weixin_41169280/article/details/127885915)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值