我有一个典型的utf-8编码问题,但到目前为止,我还没有能够解决它。
class StatsTandE(webapp2.RequestHandler):
def get(self):
conn = rdbms.connect(instance=_INSTANCE_NAME, database='origami')
cursor = conn.cursor()
cursor.execute('SELECT ui.displayName, ui.title, ui.costCenter FROM views AS v')
results = [[str(row[0]), str(row[1]), str(row[2])] for row in cursor.fetchall()]
logging.info('results identified as %s', results)
template_file_name = 'templates/stats_results.html'
template_values = {
'results': results,
}
path = os.path.join(os.path.dirname(__file__), template_file_name)
self.response.out.write(template.render(path, template_values))
conn.close()
我看到的错误是:
UnicodeEncodeError: 'ascii' codec can't encode character u'xa0' in position 4: ordinal not in range(128)
改变str(row[0])
到str(row[0]).encode('utf-8')
没有工作…
有什么建议吗?
尝试将str(row[0])
改为unicode(row[0])
更新:正如其他人所说:除非你需要,否则你甚至不应该使用unicode
和str
。试试row[0], row[1], row[2]
。当需要显示它时,执行如下操作:row[0].encode('utf8')
.