如何检测文件格式的编码

How to detect encoding of a file format

我在 s3 的存储桶中有文件,我正在以流的形式读取它们。我想检测不同文件的编码。

我使用了 chardet 库,我收到了这个错误:

TypeError: Expected object of type bytes or bytearray, got: <class 
'botocore.response.StreamingBody'>

我的代码是:

a = (obj.get()['Body'])
reader = chardet.detect(a).get('encoding')
print(reader)

还有没有其他方法可以在打开文件之前检测编码

我明白了

您需要再次使用阅读功能!

      a = (obj.get()['Body']._raw_stream).read()