1 # -*- coding: ISO-8859-1 -*- 2 3 from posix.iconv import Converter, EINVAL, EILSEQ 4 5 to_utf8 = Converter("ISO-8859-1", "UTF-8") 6 to_utf16 = Converter("ISO-8859-1", "UTF-16") 7 from_utf8 = Converter("UTF-8", "ISO-8859-1") 8 from_utf16 = Converter("UTF-16", "ISO-8859-1") 9 10 try: 11 iso = "???" 12 print iso # ??? 13 utf = to_utf8.convert(iso) 14 print utf # ?????? 15 print from_utf8.convert(utf) # ??? 16 utf = to_utf16.convert(iso) 17 print utf # ... 18 try: 19 print from_utf16.convert(utf) # ??? 20 except OSError, exc: 21 if exc.value == EINVAL: 22 print "Incomplete input", exc.arg 23 elif exc.value == EILSEQ: 24 print "Invalid input", exc.arg 25 else: 26 print exc.value, exc.arg 27 finally: 28 to_utf8.close() 29 to_utf16.close() 30 from_utf8.close() 31 from_utf16.close() 32 33 try: 34 Converter("horses", "giraffes") 35 except OSError, exc: 36 print 'Converter("horses", "giraffes"): not valid encodings; error is', exc.value