AttributeError:'int'对象没有属性'encode'

big*_*801 1 python django mechanize

我继承了一个抛出以下错误的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'
Run Code Online (Sandbox Code Playgroud)

我是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')])
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

我相信这就是说self.SetControl("status", [self.status.encode('utf-8', 'replace')]) 错误的行,但是我不知道为什么错误正在发生.代码一直在工作,因为我在6个月前继承了它并且它在我的结尾没有改变.

任何帮助,将不胜感激.

Cat*_*lus 8

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