Re: Unicode string formating
nico schrieb:
Hi,
I need to do a lot of string formating, and I have strings and/or
unicode strings and when I do the following:
"%s %s" % (u'Salut', 'H\xe4llo'), I get an exception :
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
1: ordinal not in range(128)
How can I insure I don't get an exception?
By not mixing unicode and str together. Either you decode your str to
become unicode objects using the proper encoding - or you encode the
unicode-objects before:
string = "a string"
u_string = string.decode("ascii") # or whatever you need
print u"%s %s" % (u'Salut', u_string)
Or:
salut_string = u"Salut".encode("utf-8") # whatever encoding you need
print u"%s %s" % (salut_string, "foo")
Diez
.
Relevant Pages
- Re: diferences between 22 and python 23
... >if strings had an encoding attached. ... >I would use a Unicode object to represent these characters. ... ISTM str instances seem to be playing a dual role as ascii-encoded strings ... (comp.lang.python) - Re: Question on Strings
... I just wanted to know how are strings represented in python? ... Python 2.x knows str and unicode - the former a sequence ... Strings are stored as UTF-16 or UTF-32 characters? ... (comp.lang.python) - Re: str and unicode proper usage
... If an entire application operates on Unicode strings from UI to ... the opencommand for files accepts unicode strings. ... am just wondering if there is a place where str() would have to be ... But if it's feasible, convert input data immediately to Unicode, do all your processing (including all literal strings) in Unicode, and convert back on output. ... (comp.lang.python) - Re: Unicode/UTF-8 decoding
... I don't really know how this work, but IE or Firefox browser can decode easily. ... This text looks as it has been decoded with a different encoding than was used to encode it. ... If you want to store unicode strings in the MySQL database, it has to be set up to use unicode as character set. ... While this gives the correct result for some strings, some byte codes used in UTF-8 doesn't represent a single character by themselves, so if you contine to store mis-decoded strings as unicode, you will sooner or later experience corrupted strings. ... (microsoft.public.dotnet.languages.vb) - Strange problems with encoding
... str = re.sub ... UniCode Error: ASCII decoding error: ordinal not in range ... I played with the python ... about Strings done by python, but somehow i ve missed the clue. ... (comp.lang.python) |
|