i successfully made a csv export view for a queryset but i have the
problem with some characters...
how can i encode the queryset passed to the csv writer to solve my problem?
#my csvexport.py
import csv
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from myapp.models import Project
@login_required
def projects_csv(request):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment;
filename=Projects.csv'
csv_data = Project.objects.all()
writer = csv.writer(response)
writer.writerow(['title' ,'area' , 'type', 'axis','beneficiary',
'description', 'budget', 'already paid
amount ',
'length', 'financing' ,
'total_amount_to_finance', 'percentage_to_pay' ,
'restamountto_pay', 'security', 'date'])
for project in csv_data:
writer.writerow([ project.title , project.area ,
project.type, project.axis,project.beneficiary,
project.description, project.budget,
project.already_paid_amount,
project.length, project.financing,
project.total_amount_to_finance, project.percentage_to_pay_by_tis ,
project.restamount_to_pay,
project.security, project.pub_date ])
return response
thank you...
FWIW, I just did the following:
for line in lines:
writer.writerow(
[unicode(s).encode("utf-8") for s in (
line.field1,
line.field2,
)
]
)