Python Unicode to String conversion
thijs.braem at gmail.com
thijs.braem at gmail.com
Sun Sep 16 13:11:33 EDT 2007
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 <iap... at gmail.com> 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
More information about the Python-list
mailing list