我知道stackoverflow上有很多非ascii字符问题,但由于我是一个全新的,我没有成功实现它们的运气,而且我发现整个'unicode'概念很难理解。
所以我有一个清单 -
mylist = ["apple", "samsung", "toshiba", "Don’t know", "Can’t recall"]
我想访问索引3和4处的单引号,并用撇号替换它们。
我试过了:
# -*- coding: utf-8 -*-
mylist = ["hello", "don't know", "Don’t know", "Can't recall"]
for word in mylist:
word.replace(u"’", "'")
print mylist
我收到以下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 3: ordinal not in range(128)
不确定这是否有用,但我使用的是python版本2.x,我知道如果我使用的是版本3,则可能不会出现此问题。
谢谢!
答案 0 :(得分:1)
>>> mylist = ["apple", "samsung", "toshiba", "Don’t know", "Can’t recall"]
>>> [item.replace('\xe2\x80\x99',"'") for item in mylist]
['apple', 'samsung', 'toshiba', "Don't know", "Can't recall"]
如果所有项目都已经是unicode:
>>> mylist = [u"apple", u"samsung", u"toshiba", u"Don’t know", u"Can’t recall"]
>>> [item.replace(u'’',u"'") for item in mylist]
[u'apple', u'samsung', u'toshiba', u"Don't know", u"Can't recall"]