Re: Python Unicode to String conversion

2007-09-16 Thread thijs . braem
Sorry for answering so late. Thanks a million! This code snippet
helped me solve the problem.

I think I will be using SQLAlchemy for these sorts of things from now
on though, it seems to be taking care of these things itself, on top
of being one hell of a handy ORM of course :)

thijs

On 1 sep, 09:17, iapain [EMAIL PROTECTED] wrote:
 First make sure your DB encoding is UTF-8 not the latin1

  The error I keep having is something like this:
  ERREUR:  Séquence d'octets invalide pour le codage «UTF8» : 0xe02063

 then try this:

 def smart_str(s, encoding='utf-8', errors='strict'):
 
 Returns a bytestring version of 's', encoded as specified in
 'encoding'.
 
 if not isinstance(s, basestring):
 try:
 return str(s)
 except UnicodeEncodeError:
 return unicode(s).encode(encoding, errors)
 elif isinstance(s, unicode):
 return s.encode(encoding, errors)
 elif s and encoding != 'utf-8':
 return s.decode('utf-8', errors).encode(encoding, errors)
 else:
 return s


-- 
http://mail.python.org/mailman/listinfo/python-list


Python Unicode to String conversion

2007-08-31 Thread thijs . braem
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

-- 
http://mail.python.org/mailman/listinfo/python-list