Python:替换字符串列表中的非ascii字符



我知道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,可能不会出现这个问题。

谢谢!

>>> mylist = ["apple", "samsung", "toshiba", "Don’t know", "Can’t recall"]
>>> [item.replace('xe2x80x99',"'") 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"]

相关内容

  • 没有找到相关文章

最新更新