python-3.xencodingchardet

How to detect encoding of a file format


I have files in bucket of s3 and i am reading them as stream. I want to detect the encoding of the diffrent files.

I used chardet library , i am getting this error:

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

and my code is:

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

And is there any other ways to detect the encoding before opening of a file


Solution

  • i got this

    you need to use read function again!

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