Codec 'ascii' cannot encode character u '\ u2013' at position 9: serial number not in range (128)

I am trying to import into cvs but I am getting this error

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

Unicode Error Indication

A string that cannot be encoded / decoded was :) 758-9800

I tried .encode, unicode (), etc., and nothing works, I don’t know if I need a library or something else, because I have the same code on another computer and it works 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 
+7
python django export csv
source share
2 answers

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

Alternatively, you can try using

 from __future__ import unicode_literals 

like the first line of your code.

+3
source share

I assume you are using python 2.x?

if when trying to use .encode ('utf-8') in your line when writing.

+10
source share

All Articles