AttributeError:'int'对象没有属性'encode' [英] AttributeError: 'int' object has no attribute 'encode'
问题描述
我继承了一个引发以下错误的python脚本:
I have inherited a python script that is throwing the following error:
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很新,没有相当发达的调试技能,以了解如何解决这个问题。
I'm pretty new to Python and haven't quite developed the debugging skills to know how to solve this issue yet.
以下是 photogallery.py
中的代码段在上述错误中被拒绝:
Here's the code snippet from photogallery.py
that is refrenced in the above error:
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方法
This is the SetControl method that is within the scaffolding.py
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个月前,它没有被更改我的结束。
I believe it's the line that says self.SetControl("status", [self.status.encode('utf-8', 'replace')])
that is throwing the error however I can't tell why the error is ocuring. The code has been working since I inherited it 6 months ago and it has not been altered on my end.
任何帮助将不胜感激。
推荐答案
你首先将状态
作为 int
的实例,然后尝试使用 encode
方法,因为它是一个 unicode
方法,如果要将整数转换为字符串,请使用 unicode(self.status)
然后你可以使用 encode
,尽管你最有可能不应该。
You first assert status
to be an instance of int
, and then you try to use encode
method, which it doesn't have, because it's a unicode
method. If you want to convert the integer to string, use unicode(self.status)
. Then you can use encode
on it, though you most likely shouldn't.
这篇关于AttributeError:'int'对象没有属性'encode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!