0

I want to export a CSV-file encoded in ASCII but the default is UTF-8 if I encode the strings i get the bytecode written in the csv (b'String')

 response = HttpResponse(content_type='text/csv')
 response['Content-Disposition'] = 'attachment; filename="datev_export.csv"'
 writer = csv.writer(response, delimiter=";",)
 row = ['some', 'strings']
 writer.writerow(first_row)
 return response

So how can I encode my string to ASCII without the leading b'' ?

1
  • Your code does not produce the problem for me with Django 1.9 on Python 3. The CSV file does not contain the b"" prefix, and the default encoding is ascii. Post the code and related data that produces the problem. Commented Jul 27, 2016 at 12:30

1 Answer 1

0

You can use the encode method of strings like

"asdf".encode("ascii")

for bytes there is the decode method for getting things back like

b"asdf".decode("ascii")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.