3

私はリストを手に入れましたL = [u'steve', u'micheal', u'pedro\xae']

読もうとするとエラーが発生しました。「\xae」に関係があると思います

>>> L = [u'steve', u'micheal', u'pedro\xae']
>>>
>>> for n in L:
...     print n
...
steve
micheal
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 5: ordinal not in range(128)
>>>

キャラクターをエスケープする方法はありますか?

読み取りが非常に簡単になるように必要な出力は次のとおりです。

L= ['steve', 'micheal', 'pedro']

ありがとう!

4

1 に答える 1

7

安価なソリューション

print n.encode('ascii','backslashreplace')

また

print n.encode('ascii','ignore')

しかし、Martijn Pieters のリンクを見て、エンコーディングを修正することをお勧めします...そうしないと、プログラムの他の場所でさらに問題が発生する可能性があります

于 2013-04-23T22:05:30.140 に答える