id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 10730 CSV Enconding Enhance Camilo Nova nobody "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" closed Uncategorized 1.0 invalid csv Unreviewed 0 0 0 0 0 0