python编码/解码问题
我正在尝试使用python的模板对象来创建html电子邮件。但是,当应用程序尝试发送电子邮件时,我收到错误:python编码/解码问题,python,google-app-engine,Python,Google App Engine,我正在尝试使用python的模板对象来创建html电子邮件。但是,当应用程序尝试发送电子邮件时,我收到错误: File "/base/data/home/apps/hanksandbox/1.350486862928605153/main.py", line 573, in post html = messages.email_document_html(document) File "/base/data/home/apps/hanksandbox/1.350486862928605153/m
File "/base/data/home/apps/hanksandbox/1.350486862928605153/main.py", line 573, in post
html = messages.email_document_html(document)
File "/base/data/home/apps/hanksandbox/1.350486862928605153/messages.py", line 109, in email_document_html
description = document.get_description()
File "/base/python_runtime/python_dist/lib/python2.5/string.py", line 170, in substitute
return self.pattern.sub(convert, self.template)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 763: ordinal not in range(128)
错误引用的代码如下所示:
def email_document_html(document):
email_document = Template("""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
${stylesheet}
</head>
<html>
<body>
<h1 id="mainhead">Essays</h1>
<div class="document">
<span class="info">At ${date} ${author} published:</span><br/><hr/>
<a href="${domain}/${author}/document/${filename}/" target="_blank"><h1 class="title">${title}</h1></a>
<p class="description">${description}</p>
</div>
</body>
</html>
""")
return email_document.substitute(
stylesheet=style,
date=str(document.date),
author=document.author.username,
domain=domainstring,
filename=document.filename,
title=document.title,
description = document.get_description()
)
def email\u document\u html(文档):
电子邮件\文档=模板(“”)
${stylesheet}
散文
在${date}${author}发布时:
${description}
""")
返回电子邮件\u文档。替换(
样式表=样式,
日期=str(文件日期),
author=document.author.username,
domain=domainstring,
filename=document.filename,
title=document.title,
description=document.get_description()
)
我的IDE使用UTF-8作为其字符集。我已尝试将#coding:utf-8
添加到我的文件中。我尝试在各种地方添加.encode(“utf-8”)
或.decode(“utf-8”)
,尤其是在“document.get_description()”之后。但我在抓救命稻草
有什么建议吗?到处使用
unicode
email_document = Template(u"""
...
在任何地方使用
unicode
email_document = Template(u"""
...
在模板字符串之前放置一个
u
,以指定它是unicode字符串。Python<3将这样的字符串解释为ASCII,除非另有说明
email_document = Template(u"""
...
将
u
放在模板字符串之前,以指定它是unicode字符串。Python<3将这样的字符串解释为ASCII,除非另有说明
email_document = Template(u"""
...
谢谢我简直不敢相信我因为一封小信而融化了好几个小时。谢谢!我简直不敢相信我在一个小字母
u
上融化了几个小时的大脑。