'ascii'编解码器无法在位置9编码字符u'\ u2013':序数不在范围内(128) [英] 'ascii' codec can't encode character u'\u2013' in position 9: ordinal not in range(128)

查看:103
本文介绍了'ascii'编解码器无法在位置9编码字符u'\ u2013':序数不在范围内(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入cvs,但出现此错误

I'm trying to import to cvs, but I get this error

UnicodeEncodeError at /brokers/csv/'ascii' codec can't encode character u'\u2013' in position 9: ordinal not in range(128)

Unicode错误提示

Unicode error hint

无法编码/解码的字符串是:)758–9800

The string that could not be encoded/decoded was: ) 758–9800

我尝试了.encode,unicode()等,但没有任何效果,我不知道我是否需要一个库或其他东西,因为我在另一台机器上有相同的代码并且工作正常.

I have tried .encode, unicode(), etc and nothing works, I don't know if I need a library or something else, 'cause I have the same code in other machine and is working fine.

 def exportar_a_csv_brokers(request):
     #Fecha actual
     hoy = datetime.now().date()
     #Creado el:
     creado_hoy = hoy.strftime("%m/%d/%Y")
     response = HttpResponse(mimetype='text/csv')
     response['Content-Disposition'] = 'attachment;filename=
     "Reporte de Brokers ' +  creado_hoy + '.csv"'
     response['Content-Type'] = 'text/csv; charset=utf-8'
     response.write("\xEF\xBB\xBF")

     writer = csv.writer(response)
     brokers = Broker.objects.all()
     writer.writerow(['Creado el:             ' + creado_hoy + ' '])
     writer.writerow([''])
     writer.writerow(
    ['Apellido Paterno', 'Nombre', '# Broker', '# Licencia de Seguro', 'ID Federal',  'Nombre Agencia', 'Teléfono',
     'Correo Electrónico', 'Fax', 'Calle', '# Interior', 'Colonia', 'Código Postal', 'Estado', 'Ciudad'])

for broker in brokers:
    #Imprimiendo resultados
    writer.writerow([broker.ap_paterno, broker.nombre, broker.no_broker,
                     broker.no_licencia_seguro, broker.id_federal, broker.nombre_agencia, broker.telefono,
                     broker.correo_electronico, broker.fax,
                     broker.calle, broker.no_interior, broker.colonia, broker.codigo_postal, broker.estado,
                     broker.ciudad])
return response

推荐答案

您是否正在使用lib cStringIO?用cStringIO替换StringIO后,我遇到了类似的问题.回到StringIO是解决方案.

Are you using lib cStringIO? I ran into a similar problem after replacing StringIO with cStringIO. Going back to StringIO was the solution.

此外,您可以尝试使用

from __future__ import unicode_literals

作为代码的第一行.

这篇关于'ascii'编解码器无法在位置9编码字符u'\ u2013':序数不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆