【发布时间】:2020-11-19 06:45:06
【问题描述】:
尝试浏览了一些类似的线程,但仍然感到困惑:
我有一个带有一些特殊字符的字节字符串(在我的例子中是双引号),如下所示。将其正确转换为字符串以便正确映射特殊字符的最简单方法是什么?
b = b'My groovy str\xe2\x80\x9d is now fixed'
更新:关于 decode('utf-8')
>>> b = b'My groovy str\xe2\x80\x9d is now fixed'
>>> b_converted = b.decode("utf-8")
>>> b_converted
'My groovy str\u201d is now fixed'
>>> print(b_converted)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\u201d' in position 13: ordinal not in range(128)
【问题讨论】:
标签: python string python-unicode