python:从互联网复制+粘贴撇号给了我一个奇怪的错误

标签 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

而不是

>>> 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/

相关文章:

用于 file.read() 多字节请求的 Python EOF

python - python 文档中的这段斐波那契代码与我的重新实现相比有什么问题?

python - 如何将字符串转换为字典或列表?

ios - 获取以字节为单位的UTF-8字符串长度的最佳方法?

python - 在 python 中使用挪威字母 æøå

javascript - 带有自定义图标的 Bokeh 自定义操作

python - matplotlib 将 x 轴上的刻度线命名为 1、2、4、8、16 等

c++ - 如何将代码点转换为 utf-8?

c - string concat C,有更好的解决方案吗?

Java 给定整数的字符串排列