Categories
Python Answers

How to determine the encoding of text with Python?

Spread the love

Sometimes, we want to determine the encoding of text with Python.

In this article, we’ll look at how to determine the encoding of text with Python.

How to determine the encoding of text with Python?

To determine the encoding of text with Python, we can use the python-magic package.

To install it, we run:

pip install python-magic

Then we write:

import magic

blob = open('foo.txt', 'rb').read()
m = magic.open(magic.MAGIC_MIME_ENCODING)
m.load()
encoding = m.buffer(blob)
print(encoding)

to open the foo.txt file with open.

Then we read the file with read.

Next, we call magic.open with magic.MAGIC_MIME_ENCODING and assign the returned object to m to let us call the load and buffer methods to determine the encoding of blob`.

blob has the returned file handle from read.

Conclusion

To determine the encoding of text with Python, we can use the python-magic package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *