Python Unicode to String conversion

Larry Bates larry.bates at websafe.com
Sat Sep 1 01:40:02 CEST 2007


thijs.braem at gmail.com wrote:
> Hi everyone,
> 
> I'm having quite some troubles trying to convert Unicode to String
> (for use in psycopg, which apparently doesn't know how to cope with
> unicode strings).
> 
> The error I keep having is something like this:
> ERREUR:  Séquence d'octets invalide pour le codage «UTF8» : 0xe02063
> 
> (sorry, locale is french, it means "byte sequence invalid for encoding
> <<utf8>>", the value is probably an e with one of the french accents)
> 
> I've found lots of stuff about this googling the error, but I don't
> seem to be able to find a "works always"-function just to convert a
> unicode variable back to string...
> 
> If someone could find me a solution, that'd really be a lifesaver.
> I've been losing hours and hours over this one :s
> 
> thijs
> 
Question is, what should it be converted to when there is no corresponding
ascii character to map the offending character to?  Do you want to throw it 
away?  Your expectation "works always" is very ill-defined.  Write a small 
function to process the strings yourself and set up a substitution dictionary to 
do the conversion yourself.

-Larry



More information about the Python-list mailing list