Reputation: 1246
I have a text file with the following contents:
Guadalajara
Culiacán
Juárez
Ecatepec
I want to convert all of these to unicode. I tried using:
unicode(INSERT WORD FROM TEXT FILE)
But I get the error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 6: ordinal not in range(128)
Is there anyway of converting a string from a text file to unicode?
Upvotes: 0
Views: 65
Reputation: 336108
You need to know the file's encoding. If you do, open the file with codecs.open()
and you'll automatically get proper Unicode objects:
import codecs
with codecs.open("myfile.txt", encoding="utf-8") as infile:
text = infile.read()
You can also open the file "normally" and then manually decode its contents - you still need to know what the encoding is, though:
with open("myfile.txt") as infile:
text = infile.read()
uni = text.decode("utf-8")
Upvotes: 1