Outputting Shift-JIS CSV with Django

DjangoPython
2016-06-20 08:35 (9 years ago)
Outputting Shift-JIS CSV with Django

Python 3, Django 1.9

It might be easier and less troublesome to write in Unicode to StringIO first and then convert to CP932 all at once.

class CSVView(View):
def get(self, request, **kwargs):

response = HttpResponse(content_type='text/csv; charset=Shift-JIS')
response['Content-Disposition'] = \
'attachment; filename=items.csv'
sio = io.StringIO()
writer = csv.writer(sio)
writer.writerow(self.header)
for row in self.get_rows():
writer.writerow(row)
response.write(sio.getvalue().encode('cp932'))
return response
Please rate this article
Current rating: 4.1 (9)
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive