[英]Returning AttributeError: 'int' object has no attribute 'encode'
[英]AttributeError: 'int' object has no attribute 'encode'
我继承了引发以下错误的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
,尽管您极可能不应该这样做。
使用repr()
函数。 此函数可以处理unicode,utf,null和int。 此函数的一个很好的部分是,在将unicode(如encode('utf-8')
那样)或ascii转换为字符串时,此函数不会丢失任何值。 此功能还为制图表达提供了大小限制,因此您可以更灵活地处理对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.