How to detect the Text Encoding of a File in Python

Knowing the text encoding for a given file is an important step in its processing. So how can we differentiate between ASCII, UTF7, UTF8, UTF16, UTF32, and BigEndianUnicode? Well, we could spend all afternoon (and probably evening as well) working on the problem. Or we could just use an API.

Pip install the client with this command here.

pip install cloudmersive-convert-api-client

Now for our function call. To use edit_text_text_encoding_detect, we will first create a new API instance. Here’s how that works:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditTextApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Detect text encoding of fileapi_response = api_instance.edit_text_text_encoding_detect(input_file)pprint(api_response)except ApiException as e:print("Exception when calling EditTextApi->edit_text_text_encoding_detect: %s\n" % e)

And there you have it. Your return will be the name of the text encoding, simple as that.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store