简体   繁体   中英

Django - Export ASCII CSV

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'' ?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM