UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-12: ordinal not in range(128)
>>> u1 = u"これはユニコード型文字列です"
>>> type(u1)
<type 'unicode'>
>>> s1 = "これはふつうの文字列です"
>>> type(s1)
<type 'str'>
# EUC-JPなソースコードでは
>>> len("あ")
2
# UTF-8なソースコードでは
>>> len("あ")
3
# unicode型なら
>>> len(u"あ")
1
# Shift-JISなソースコードでは
>>> re.match("すも+", "すもももももももももももももももももも")
→"すも"にマッチする
# unicode型なら
>>> re.match(u"すも+", "すもももももももももももももももももも")
→"すもももももももももももももももももも"にマッチする
print u1.encode('utf_8')
>>> s1 == u1.encode('utf_8')
True
>>> s1 == u1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
u1.encode()
s1.decode()
unicode(s1)
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
u1.encode('ascii')
s1.decode('ascii')
unicode(s1, 'ascii')
>>> s1.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128)
print u1.encode('utf_8')
print u1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
print sys.stdout.encoding
print u"日本語"
$ LANG=ja_JP.UTF-8 ./nihongo.py
UTF-8
日本語
$ LANG=C ./nihongo.py
ANSI_X3.4-1968
Traceback (most recent call last):
File "./nihongo.py", line 5, in ?
print u"日本語"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
$ #例1)上記のnihongo.pyを実行し、パイプに出力する
$ ./nihongo.py | cat
Traceback (most recent call last):
File "./nihongo.py", line 5, in ?
print u"日本語"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
None
$ #例2)LANGは無視される
$ LANG=ja_JP.UTF-8 ./nihongo.py | cat
Traceback (most recent call last):
File "./nihongo.py", line 5, in ?
print u"日本語"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
None
#!/usr/bin/env python
# -*- coding: utf-8 -*-
f = open('fairu', 'w')
f.write(u"日本語")
$ LANG=ja_JP.UTF-8 ./nihongofile.py
Traceback (most recent call last):
File "./nihongofile.py", line 4, in ?
f.write(u"日本語")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
import sys, codecs
sys.stdout = codecs.EncodedFile(sys.stdout, 'utf_8')
import sys, codecs
sys.stdout = codecs.EncodedFile(sys.stdout, 'utf_8')
print u"日本語"
sys.stdout = codecs.lookup('utf_8')[-1](sys.stdout)
print u"日本語"
f = open('fairu', 'w')
f = codecs.lookup('utf_8')[-1](f)
f.write(u"日本語")
import locale
enc = locale.getpreferredencoding()
import codecs
sys.stdout = codecs.EncodedFile(sys.stdout, enc)
>>> "名前は%s、%d才です。" % (u"HDE", 12)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 2: ordinal not in range(128)
>>> "名前は%s、%d才です。" % ("HDE", 12)
'\xe5\x90\x8d\xe5\x89\x8d\xe3\x81\xafHDE\xe3\x80\x8112\xe6\x89\x8d\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82'
# -*- coding: utf_8 -*-
# vim:fileencoding=utf_8
Listed below are links to blogs that reference this entry:PythonのUnicodeEncodeErrorを知る.
TrackBack URL for this entry:https://lab.hde.co.jp/blog/mt-tb.cgi/56
追加で書いておくと、Python2系では先頭行に# -*- coding: utf_8 -*- を書いて置かないとソースコードがUTF-8であると認識してくれないが、Python3系ではUTF-8に統一されるようだ。エンコードがある程度固定なら以下のような感じで自動エンコードできるのでは?...Read More
: \'ascii\' codec can\'t encode characters in position 6-32: ordinal not in range(128) このエラーメッセージをみて、ハッと心を奪われた同士諸君、こんばんわ。そして、: coercing to Unicode: need string or buffer, NoneType found このエラーメッセージを見るたびに悪夢のリフレインに苦しむ戦士のみなさま、ようこそおいでくださった。...Read More
This page contains a single entry by rgoura published onAugust 18, 2008 9:00 AM.
Yahoo! Music API で遊んでみる was the previous entry in this blog.
日経225のSPF対応状況(2008年8月) is the next entry in this blog.
Find recent content on themain index or look in thearchives to find all content.