#10730: CSV Enconding Enhance ---------------------------+------------------------------------------------ Reporter: camilonova | Owner: nobody Status: new | Milestone: Component: Uncategorized | Version: 1.0 Keywords: csv | Stage: Unreviewed Has_patch: 0 | ---------------------------+------------------------------------------------ Hi, i has to make a function to export a model data to a CSV file, i follow the docs, and works great, but later, when users began to use special characters in a field of the model, the function fails, this is the original function:
writer = csv.writer(response) writer.writerow(['ID', 'NIT', 'Nombre', 'Direccion', 'Telefono', 'Ciudad']) for cliente in Cliente.objects.all(): writer.writerow([ cliente.id, cliente.nit, cliente.nombre, cliente.direccion_principal, cliente.telefono_principal, cliente.ciudad ]) When someone save a value like "niños por la paz en día" the function raises a " Exception Value: 'ascii' codec can't encode character u'\xd1' in position 15: ordinal not in range(128) ", i look in many places for the answer, and found using enconde() may works, and eventually i solve the problem like this: writer = csv.writer(response) writer.writerow(['ID', 'NIT', 'Nombre', 'Direccion', 'Telefono', 'Ciudad']) for cliente in Cliente.objects.all(): writer.writerow([ cliente.id, cliente.nit, cliente.nombre.encode('utf-8'), cliente.direccion_principal.encode('utf-8'), cliente.telefono_principal, cliente.ciudad ]) Using encode('utf-8') for the fields that maybe user has special characters. You guys, can do this better or inside django, the encode thing by default, just for saving time and make sure the code works everywhere? Thanks -- Ticket URL: <http://code.djangoproject.com/ticket/10730> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---