Monday, April 29, 2013

Re: Upload csv with utf-8

Hi! Check this out https://gist.github.com/eightysteele/1174811

In short, value.decode('utf-8') must be used to prepare value, that was read from file, encoded with utf-8 for processing.


2013/4/29 Hélio Miranda <helio13m@gmail.com>
I have a problem that is to read the csv file that contain words with accents.
I have the following code in my application, but do not know how by working with utf-8.
Someone can help me?

Código (Python):
def csv_upload(request):
    if request.method == 'POST':

            gen = Genre.objects.all()
            genres = dict(Genre.objects.all().values_list('GenreType', 'id'))
           
            for obj in gen:
                    genres[obj.GenreType] = obj.id
                   
            file = csv.DictReader(request.FILES['file'], delimiter=',', quotechar='"')

            for line in file:              
                    report = Movie()
                   
                    if not line["IdGenre"in genres:
                            rep = Genre()
                            rep.GenreType = line["IdGenre"]
                            rep.save()
                            genres[rep.GenreType] = rep.id;
                           
                            report.MovieTitle = line["MovieTitle"]
                            report.MovieDuration = line["MovieDuration"]
                            report.save()
                           
                    else:
                            report.MovieTitle = line["MovieTitle"]
                            report.MovieDuration = line["MovieDuration"]
                            report.save()
    return render_to_response('index.html', {},
                                                          context_instance=RequestContext(request))

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment