-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_encoding.py
More file actions
executable file
·52 lines (40 loc) · 1.52 KB
/
fix_encoding.py
File metadata and controls
executable file
·52 lines (40 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""
Script to fix encoding of SGML files from ISO-8859-1 to UTF-8
The original files were encoded in ISO-8859-1, which caused issues with
special characters like accented letters.
"""
import os
import glob
from pathlib import Path
def fix_file_encoding(file_path, source_encoding, target_encoding):
"""Convert a file from source encoding to target encoding"""
try:
with open(file_path, 'r', encoding=source_encoding) as file:
content = file.read()
with open(file_path, 'w', encoding=target_encoding) as file:
file.write(content)
print(f"Fixed encoding for: {file_path}")
except Exception as e:
print(f"Error processing {file_path}: {e}")
def main():
# Get the directory where the script is located
script_dir = Path(__file__).parent
efe_dir = script_dir / 'efe'
if not efe_dir.exists():
print(f"Error: Directory '{efe_dir}' not found!")
return
# Find all SGML files in the directory
sgml_files = glob.glob(str(efe_dir / "*.sgml"))
if not sgml_files:
print("No SGML files found in the efe directory!")
return
print(f"Found {len(sgml_files)} SGML files to process...")
print("-" * 50)
for file_path in sorted(sgml_files):
# this is the important part
fix_file_encoding(file_path, 'iso-8859-1', 'utf-8')
print("-" * 50)
print(f"Completed processing {len(sgml_files)} files.")
if __name__ == "__main__":
main()