I have a problem reading a txt file to insert in the mysql db table, te sniped of this code:
file contains the in first line: “aclaración“
archivo = open(‘file.txt’,”r”)
for line in archivo.readlines():
….body = body + line
model = MyModel(body=body)
model.save()
i get a DjangoUnicodeDecodeError:
‘utf8’ codec can’t decode bytes in position 8: invalid data. You passed in ‘aclaraci\xf3n’ (type ‘str’)
Unicode error hint
The string that could not be encoded/decoded was: araci�n.
I tried to body.decode(‘utf-8’), body.decode(‘latin-1’), body.decode(‘iso-8859-1’) without solution.
Can you help me please? Any hint is apreciated 🙂
Judging from the
\xf3code for ‘ó’, it does look like the data is encoded in ISO-8859-1 (or some close relative). Sobody.decode('iso-8859-1')should be a valid Unicode string (you don’t specify what “without solution” means — what error message do you get, and where?); if what you need is a utf-8 encoded bytestring instead,body.decode('iso-8859-1').encode('utf-8')should give you one!