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
Related posts
Please rate this article (No signup or login required)
Current rating: 4.1 (9)
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.