I have some external data I need to import. How do I encode the input string as unicode/utf8?
Here is an example of a probematic line
>>>’Compa\xf1\xeda Dominicana de Tel\xe9fonos, C. por A. – CODETEL’.encode(“utf8”)
Traceback (most recent call last):
File “”, line 1, in
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xf1 in position 5: ordinal not in range(128)
.encode("utf8")expects the source to be a unicode string. You are using it with a “regular” string which has “ascii” encoding by default. You should do something like:original_string.decode('original_encoding').encode('utf-8')In your case my guess would be: