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)

See Also