【问题标题】:get standard encodings out of python [duplicate]从python中获取标准编码[重复]
【发布时间】:2019-07-07 02:53:15
【问题描述】:

所以我有一个字节对象,但不确定它的编码,但知道它不是 utf-8:

a.decode('utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9a in position 0: invalid start byte

我想做的是这样的:

for encoding in encodings:
    try:
        a.decode(encoding)
        print("This is it!", encoding)
    except Exception:
        pass

如何让 Python 为您提供将进入 .decode 的所有内容作为列表 encodings 以便我可以将其插入其中?

【问题讨论】:

  • github.com/tripleee/8bit 有执行此操作的代码。我记得以前曾多次看到此类问题,但无法立即找到好的重复项。
  • 顺便说一句,您没有退出,然后您会找到编码。但是不,许多编码可以成功解码字节数组。大多数 1 字节编码没有结构,因此每个都可以解码您的字符串。你需要变得更聪明,并且可能不会重新发明轮子。

标签: python python-3.x encoding


【解决方案1】:

你可以像这样得到它们:

import encodings
all_of_encodings = encodings.aliases.aliases.keys()

for encoding in all_of_encodings:
    # do what you want

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
相关资源
最近更新 更多