def export_csv(request):
response = HttpResponse(mimetype = 'text/csv')
response['Content-Disposition'] = 'attachment; filename=exported.csv'
writer = csv.writer(response)
data = [['First-Name','Last-name'],['Foo','baar']]
for entry in data:
writer.writerow(entry)
return response
Above code is written for outputting csv file from django. Problem I am facing is the exported/outputted file's content are not displaying in different boxes in csv file editor(s). For each entry in data (list) it is getting printed in same box rather it should interpret each entry's value(First-Name, Last-Name) in different box.
Actual result in csv file:
|First-Name,Last-Name |
|Foor,bar |
Expected:
|First-Name |Last-Name |
|Foo |Baar |
How can I get mechanism by which it will export the file independent on the list size and its contents?
writer = csv.writer(response, dialect='excel')- that may help in your case.