我了解关于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,则可能不会发生此问题。
1 回答
温温酱
TA贡献1752条经验 获得超4个赞
>>> 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"]
添加回答
举报
0/150
提交
取消