我需要能够在我的代码中使用来自网站的复制+粘贴字符串。该网站的编码是unicode (utf-8)。字符串
'''I’ve held others before'''
是复制+粘贴的,并且有一个“有趣的”撇号。当我尝试替换这个撇号时
my_string = '''I’ve held others before'''
my_string.replace('’', "'")
print(my_string)
我还是明白了
>>> I’ve held others before
而不是
>>> I've held others before
我无法使用带有有趣撇号的字符串,因为稍后在我的代码中它给了我这个错误:
'ascii' codec can't decode byte 0xe2 in position 2: ordinal not in range(128)
我尝试添加两者
my_string.decode('utf-8')
my_string.encode('utf-8')
但他们似乎什么也没做。有什么想法吗?
最佳答案
字符串在Python中是不可变的,你需要将str.replace
的结果再次赋回给变量。
>>> my_string = '''I’ve held others before'''
>>> my_string = my_string.replace('’', "'")
>>> my_string
"I've held others before"
最好对 unicode 字符串使用 u'...'
前缀:
>>> u'''Joey’s house'''.replace(u'’', "'")
"Joey's house"
在文件顶部添加此行以消除这些解码错误:
# -*- coding: utf-8 -*-
关于python:从互联网复制+粘贴撇号给了我一个奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677270/