2011-11-14 48 views
1

我繼承了一個Python腳本,拋出了以下錯誤:AttributeError的:「詮釋」對象有沒有屬性「編碼」

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的代碼段在上述錯誤refrenced:

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')]) 

這是SetControl方法是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個月前我繼承了代碼以來,代碼一直在運行,並且在我的結尾沒有更改。

任何幫助,將不勝感激。

+2

這不是Python 3.x,因爲Ellington和Django只是Python 2.x。 –

+1

這段代碼似乎不太可能在6個月內保持不變。保存一個奇怪的屬性或'__ {g,s} etattr__' hack使得'self.status = status'和'self.status'表現異常,您在這裏展示的代碼要麼拋出異常,要麼嘗試'.encode'一個對象,它是'int'的一個實例。 – delnan

+0

即使self.status是一個unicode對象,self.status.encode中的'replace'('utf-8','replace')也是邪惡的,因爲它會隱藏一個非常糟糕的「unicode」對象。一個包含一個不成對的替代品。 –

回答

7

你先斷言status是的int一個實例,然後您嘗試使用encode方法,它沒有,因爲它是一個unicode方法。如果要將整數轉換爲字符串,請使用unicode(self.status)。然後你可以使用encode,儘管你很可能不應該這樣做。

+0

感謝您的提示,我會試試看。另外,你爲什麼說「你很可能不應該」? – bigmike7801

+0

@ bigmike7801:你看過SetControl方法,看看它期望什麼嗎? –

+0

我已經將SetControl的代碼添加到上面的問題中,但無法查看可能導致問題的任何內容。 – bigmike7801

0

使用repr()功能。這個函數可以處理unicode,utf,null和int。這個函數的好處在於,在將unicode(如encode('utf-8'))或ascii轉換爲字符串時,此函數不會丟失任何值。此外,該功能還爲表示提供了大小限制,因此您可以在對象上擁有更大的靈活性。

相關問題