Converting Unicode to Local Strings


print uniStr.encode('utf-8') print uniStr.encode('utf-16') print uniStr.encode('iso-8859-1') asciiStr =asciiStr.translate( \     string.maketrans('\xF1','n'), '') print asciiStr.encode('ascii')

The Python language provides a simple encode(encoding) method to convert unicode strings to a local string for easier processing. The encoding method takes only encoding such as utf-8, utf-16, iso-8859-1, and ascii as its single argument and returns a string encoded in that format.

Strings can be converted to unicode by several different methods. One is to define the string as unicode by prefixing it with a u when assigning it to a variable. Another is to combine a unicode string with another string. The resulting string will be unicode. You can also use the decode(encoding) method to decode the string. The decode method returns a unicode form of the string.

Note

The ASCII encoding allows only for characters up to 128. If your string includes characters that are above that range, you will need to translate those characters before encoding the string to ASCII.


import string locStr = "El " uniStr = u"Ni\u00F1o" print uniStr.encode('utf-8') print uniStr.encode('utf-16') print uniStr.encode('iso-8859-1') #Combine local and unicode results #in new unicode string newStr = locStr+uniStr print newStr.encode('iso-8859-1') #ascii will error because character '\xF1' #is out of range asciiStr = newStr.encode('iso-8859-1') asciiStr =asciiStr.translate(\     string.maketrans('\xF1','n'), '') print asciiStr.encode('ascii') print newStr.encode('ascii')


unicode_str.py

Niño ÿN|I|ñ|o Niño El Niño El Nino Traceback (most recent call last):   File "C:\books\python\CH2\code\unicode_str.py", line 19, in ?     print newStr.encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode  character u'\xf1' in position 5: ordinal not in  range(128)


Output from unicode_str.py code



Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net