detect_encoding method

detect_encoding

Detects the encoding of text TXT, TSV, and CSV files by path.

def detect_encoding(cls, file_path):
    ...
Parameter Type Description
file_path str The file name or file path.

Returns: str or None: The detected encoding name, or None when detection fails.

Example

from groupdocs.viewer import FileType

encoding = FileType.detect_encoding("message.txt")
if encoding:
    print(f"Detected encoding: {encoding}")
else:
    print("Failed to detect encoding.")

detect_encoding

Attempts to detect the encoding of text files (TXT, TSV, CSV) from a stream.

def detect_encoding(cls, stream):
    ...
Parameter Type Description
stream io.RawIOBase The file stream.

Returns: Encoding or None: The detected encoding, or None if detection fails.

Example

from groupdocs.viewer import FileType

with open("message.txt", "rb") as stream:
    encoding = FileType.detect_encoding(stream)
    print(encoding)

Aliases

These names call detect_encoding too, limited to the overloads listed.

Alias Accepts
detect_encoding_file detect_encoding_file(file_path)
detect_encoding_string detect_encoding_string(file_path)
detect_encoding_stream detect_encoding_stream(stream)
detect_encoding_streams detect_encoding_streams(stream)

See Also