How to store ASCII encoded python string?

Fredrik Lundh fredrik at pythonware.com
Mon Aug 28 15:27:12 EDT 2006


micahc at gmail.com wrote:

> I currently have a Python program that reads in emails from a POP3
> server.  As soon as the message is read in it is fed directly into a
> PostgreSQL database for storage.  Later, it is broken down into it's
> parts and displayed to the user.
> 
> My problem is that when I try to pass "\tsome text\xa7some more text\n"
> into the database it gives me a unicode decode error.

"\xa7" is not a valid ASCII character, so that's not really an "ASCII 
encoded" string.

looks like your database expects Unicode strings, but you're passing in 
binary data.  to solve this, you can:

1) change the database table to use a "blob" field instead of a text field

or

2) configure the database interface to pass 8-bit strings through to the 
database engine (if possible; see the database interface documentation 
for details)

or

3) convert the data to Unicode before passing it to the database 
interface, and leave it to the interface to convert it to whatever 
encoding your database uses:

     data = ... get encoded string from email ...
     text = data.decode("iso-8859-1")
     ... write text to database ...

</F>




More information about the Python-list mailing list