2016-12-08 52 views
0

在下面我的片段,我在處理文本,多數民衆贊成的字符串:Déclaration.pngPython的編碼 - 錯誤:「Latin-1的」編解碼器不能編碼字符

我返回描述爲Unicode:

return self.render_json(request, {..."description": u''.join((instance.description)),..}) 

在另一功能中,我使用以上如下的描述:

if document.description: 
    file_name = document.description.split(".") 
    file_name = "{}.{}.{}".format(
     "_".join(file_name[:-1]), 
     str(document.id), 
     file_name[-1] 
    ) 

file_name是:[u'De\u0301claration', u'png']

當我嘗試FILE_NAME我收到以下錯誤.format():

error: 'latin-1' codec can't encode character u'\u0301' in position 2: ordinal not in range(256) 

任何想法?

+0

你想要什麼輸出看起來像 – Navidad20

+0

我想是「德\ u0301claration.123456 .png' – Brandon

回答

0

「{}。{}。{}」是一個字符串,但您嘗試使用unicode填充它。

使用

... 
file_name = u"{}.{}.{}".format(
... 

代替

也看看這個漂亮的談話:https://www.youtube.com/watch?v=sgHbC6udIqc

+0

我剛剛更新我的問題,說我嘗試過並得到相同的錯誤。但是,當我在普通的python shell中這樣做時,它都可以工作。謝謝,我會看看談話 – Brandon

+0

也許你必須重新啓動服務器 – dnalow

相關問題