Python + PostgreSQL + strange ascii = UTF8 encoding error
- by Claudiu
I have ascii strings which contain the character "\x80" to represent the euro symbol:
>>> print "\x80"
€
When inserting string data containing this character into my database, I get:
psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0x80
HINT: This error can also happen if the byte sequence does not match the encodi
ng expected by the server, which is controlled by "client_encoding".
I'm a unicode newbie. How can I convert my strings containing "\x80" to valid UTF-8 containing that same euro symbol? I've tried calling .encode and .decode on various strings, but run into errors:
>>> "\x80".encode("utf-8")
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
"\x80".encode("utf-8")
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)