【发布时间】:2021-08-06 20:46:46
【问题描述】:
python2 的 unicode(v, errors='ignore') 的确切 python3 等价物是什么?
注意:
-
v是任何字符串(例如six.string_types) -
errors='ignore'很重要
谢谢!
【问题讨论】:
标签: python-3.x string python-2.7 python-unicode
python2 的 unicode(v, errors='ignore') 的确切 python3 等价物是什么?
注意:
v 是任何字符串(例如six.string_types)errors='ignore' 很重要谢谢!
【问题讨论】:
标签: python-3.x string python-2.7 python-unicode
如果v 已经是six.string_types 的一部分,则v 的唯一选项是str。 Python 3 str 等同于 Python 2 unicode,因此这种转换是多余的,因为您只是将 str 转换为 str。
如果您想将bytes(不是six.string_types 的一部分)转换为str,那么您需要b.decode("utf-8", errors="ignore"),或者只是str(b, errors = "ignore")。
【讨论】: