僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。
2.6.5 では簡単にできたけど、3.1.2では、少なくとも同じ方法ではできなかった。
$ python Python 2.6.5 (r265:79063, Jun 17 2010, 21:19:08) [GCC 4.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True True >>> False False >>> True, False = False, True >>> True False >>> False True
$ python3 Python 3.1.2 (r312:79147, Jun 17 2010, 22:23:31) [GCC 4.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> True, False = False, True File "<stdin>", line 1 SyntaxError: assignment to keyword
>>> 'ASCIIじゃ無いの'.encode('ascii','xmlcharrefreplace').decode() 'ASCIIじゃ無いの'
encode()の、二つ目の引数のデフォルトはUnicodeEncodeError。
他、'ignore'や'replace'が指定できる、らしい。
16進表示は、少々ややこしいかもしれない。
リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。