Skip to main content
 首页 » 编程设计

python之属性错误 : 'int' object has no attribute 'encode'

2025年05月04日13pander-it

我继承了一个抛出以下错误的 python 脚本:

root        : ERROR    Unexpected exception encountered in application 'ImagesForWeb' 
Traceback (most recent call last): 
  File "build/bdist.linux-i686/egg/columbiancommon/scaffolding/consoleapp.py", line 169, in run_safe 
    self.run(**kwargs) 
  File "/var/scripts/ImagesForWeb/imagesforweb.py", line 102, in run 
    gallery = columbiancommon.EllingtonPhotoGallery(configobj = self.cmgr) 
  File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 51, in __init__ 
    self.Reload() 
  File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 128, in Reload 
    self.SetStatus(self.status) 
  File "build/bdist.linux-i686/egg/columbiancommon/ellington/photogallery.py", line 68, in SetStatus 
    self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 
AttributeError: 'int' object has no attribute 'encode' 

我是 Python 的新手,还没有完全掌握如何解决这个问题的调试技巧。

这是 photogallery.py 中的代码片段,在上述错误中被引用:

def SetStatus(self, status): 
    """ 
    Sets the publication status of this photo gallery. 
 
    Expects to be set to an integer constant, shortcuts include:: 
 
        EllingtonPhotoGallery.LIVE 
        EllingtonPhotoGallery.DRAFT 
        EllingtonPhotoGallery.DELETED 
        EllingtonPhotoGallery.UNREVIEWED 
 
    """ 
    if(not isinstance(status, int)): 
        raise EllingtonMechanizeException("Unexpected status value.  Please use a status constant.") 
    self.status = status 
    self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 

这是 scaffolding.py 中的 SetControl 方法

def SetControl(self, control_name, control_value_unclean): 
    """ 
    Raw access to the mechanize method of setting controls to specific values. 
 
    **WARNING** Do not use this unless you have a really good reason to do so-- `EllingtonMechanizeScaffolding.SetControlToValue` 
    and `EllingtonMechanizeScaffolding.SetControlToValueSafe` are much more elegant solutions. 
 
    :Parameters: 
        - `control_name`: The name of the control you're trying to assign a value to. 
        - `control_value_unclean`: The value to assign to said control.  Either a boolean value,  
 
    """ 
    self.browser[control_name] = control_value_unclean 
    return True 

我相信这是说 self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 抛出错误但是我不知道为什么会发生错误。自从我在 6 个月前继承它以来,代码一直在工作,并且我没有改变它。

如有任何帮助,我们将不胜感激。

请您参考如下方法:

你首先断言 status 是 int 的实例,然后你尝试使用 encode 方法,它没有,因为它是一个 unicode 方法。如果要将整数转换为字符串,请使用 unicode(self.status)。然后您可以对其使用 encode,尽管您很可能不应该这样做。