【问题标题】:What is the exact python3 equivalent of python2's unicode(v, errors='ignore')?python2的unicode(v,errors ='ignore')的确切python3等价物是什么?
【发布时间】:2021-08-06 20:46:46
【问题描述】:

python2 的 unicode(v, errors='ignore') 的确切 python3 等价物是什么?

注意:

  1. v 是任何字符串(例如six.string_types
  2. errors='ignore' 很重要

谢谢!

【问题讨论】:

    标签: python-3.x string python-2.7 python-unicode


    【解决方案1】:

    如果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")

    【讨论】:

    • 感谢您在第 2 段中纠正我的理解 - 有效!
    猜你喜欢
    • 1970-01-01
    • 2012-01-20
    • 2012-03-20
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 2017-06-11
    • 2023-03-22
    • 2021-09-15
    相关资源
    最近更新 更多