python:copy+;从网上粘贴撇号给了我一个奇怪的错误

python:copy+;从网上粘贴撇号给了我一个奇怪的错误,python,string,utf-8,ascii,Python,String,Utf 8,Ascii,我需要能够使用复制+粘贴字符串从一个网站在我的代码。该网站的编码是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 而不是

我需要能够使用复制+粘贴字符串从一个网站在我的代码。该网站的编码是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中是不可变的,您需要将
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 -*-

粘贴字符串的页面的编码是什么?我认为是unicode(utf-8)。粘贴字符串的页面的编码是什么?我认为是unicode(utf-8)。