Outputting Shift-JIS CSV with Django

Django Python
2016-06-20 17:35 (8 years ago) ytyng

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
Current rating: 4.1

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011