Tuesday, April 30, 2013

Problem enconding

I have a problem in my application that I am not able to solve, which is as follows:
In my application I have the option of entering the bd data from a csv file, is part is ok, the problem is that the data come in csv utf-8 and I compare with data that will seek to mongodb that come in latin iso or do not know well.

I'll fetch the genres of movies and save it for later if there is already comparing the genre of the movie or not and then insert.
But when you'll get the mongo station like this: Com \ \ xe9dia
And those who come in csv station like this: Com \ \ xc3 \ \ xa9dia
This for the Comedy genre.

How can I solve this problem?
My code is here:
Código (Python):
def csv_upload(request):
        if request.method == 'POST':
                gen = Genre.objects.all()
                genres = dict(Genre.objects.all().values_list('GenreType', 'id'))
                print genres
                for obj in gen:
                        genres[obj.GenreType] = obj.id
                  
                file = request.FILES['file']  
                dialect = csv_mod.Sniffer().sniff(codecs.EncodedFile(file,"utf-8").read(1024))
                file.open()
                csv = csv_mod.DictReader( codecs.EncodedFile(file,"utf-8"), dialect=dialect )
                for line in csv:                          
                        report = Movie()
                  
                        genall = line["IdGenre"].split(',')
                        print genall
                        movieGenres = []
                  
                        for cc in genall:
                                if not cc in genres:
                                        rep = Genre()
                                        rep.GenreType = cc
                                        rep.save()
                          
                                        genres[rep.GenreType] = rep.id;
                                movieGenres.append(genres[cc])
                          
                        report.MovieTitle = line["MovieTitle"]
                        report.IdGenre = movieGenres
                        report.MovieYear = line["MovieYear"]
                        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.
 
 

No comments:

Post a Comment