deepankgupta
deepankgupta

Reputation: 1

Incorrect string value: '\xE0\xB9\x82\xE0\xB8\xA3...' for column 'text' at row 1

In my Django app I am reading a text file and uploading it as a LongText field in the mysql database. My database supports UTF-8 encoding but I get the following error:

Exception Type: Warning at /upload Exception Value: Incorrect string value: '\xE0\xB9\x82\xE0\xB8\xA3...' for column 'text' at row 1

The code which converts it to the unicode in python is here:

fileobj = self.request.FILES.get('filepath', None)
filetext = unicode(fileobj.read(), 'utf-8') 
uploadedText = models.UploadedText()
uploadedText.text = filetext uploadedText.save()

Upvotes: 0

Views: 1550

Answers (1)

unni
unni

Reputation: 2991

Make sure that you have set your database collation to utf8_general_ci and that your UploadedText model looks something like this:

class UploadedText(models.Model):
  text = models.CharField(max_length=500)

Then try out this code:

fileobj = self.request.FILES.get('filepath', None)
filetext = fileobj.read() 
uploadedText = models.UploadedText()
uploadedText.text = filetext 
uploadedText.save()

You don't need to explicitly convert the contents of the file to unicode if they are already in unicode. Also test this code for a file with only ascii contents to see if you get the required response.

Hope this helps. If you encounter any further errors please POST them. Cheers

Upvotes: 2

Related Questions