2016-12-08 53 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

相关问题