ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/FileEncodingLib.py
Revision: 890
Committed: Sat May 24 02:09:35 2025 UTC (10 months ago) by nino.borges
Content type: text/x-python
File size: 1351 byte(s)
Log Message:
A set of functions that will help with file encoding detection and issues.
This is version 2 of this library which **requires carset-normalizer be installed**.  Previously used chardet.

File Contents

# User Rev Content
1 nino.borges 890 """
2    
3     FileEncodingLib
4    
5     Created by:
6     Emanuel Borges
7     07.12.2019
8    
9     A set of functions that will help with file encoding detection and issues.
10     This is version 2 of this library which **requires carset-normalizer be installed**. Previously used chardet.
11    
12     """
13    
14    
15     import os, sys
16     from charset_normalizer import from_path
17    
18    
19     def DetectFileEncoding(fileName):
20     """Attempts to detect the file encoding. Returns encoding and prints the confidence level. fileName should also include the full path to the file."""
21    
22     ## First verify if charset_normalizer is installed.
23     if 'charset_normalizer' in sys.modules:
24     pass
25     else:
26     print("charset_normalizer is not intalled. Please install with PIP before using.")
27     return None
28    
29     ## Attempt to determin encoding.
30     try:
31     testResult = from_path(fileName).best()
32     if testResult:
33     fileEncoding = testResult.encoding
34     print(f"File appears to have the encoding {fileEncoding} (Confidence: {testResult.encoding_aliases})")
35     return fileEncoding
36     else:
37     print("FAILED: Unable to detect encoding from this file. Suggest trying common encodings 'utf-8' or 'latin1'.")
38     return None
39     except Exception as e:
40     print(f"Other error occurred: {e}")
41     return None
42