[英]Python encoding issue (ascii > unicode)
我遇到了典型的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')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.