File size: 2,652 Bytes
c922f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Script to find and fix Unicode characters in app.py 
"""
import re
import os

def find_unicode_chars(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Find all non-ASCII characters
        unicode_chars = set(re.findall(r'[^\x00-\x7F]', content))
        
        if unicode_chars:
            print(f"Found {len(unicode_chars)} Unicode characters in {file_path}:")
            for char in unicode_chars:
                print(f"  '{char}' (U+{ord(char):04X})")
            return unicode_chars
        else:
            print(f"No Unicode characters found in {file_path}")
            return None
    except Exception as e:
        print(f"Error reading {file_path}: {e}")
        return None

def replace_unicode_chars(file_path, output_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        # Replace common Unicode characters with ASCII alternatives
        replacements = {
            '✓': '[PASS]',
            '✗': '[FAIL]',
            '❌': '[ERROR]',
            '✅': '[SUCCESS]',
            '⚠️': '[WARNING]',
            '→': '->',
            '←': '<-',
            '…': '...',
            '–': '-',
            '—': '--',
            ''': "'",
            ''': "'",
            '"': '"',
            '"': '"',
            '•': '*',
            '©': '(c)',
            '®': '(R)',
            '™': '(TM)',
            '°': ' degrees',
            '±': '+/-',
            '×': 'x',
            '÷': '/',
            '≤': '<=',
            '≥': '>='
        }
        
        for char, replacement in replacements.items():
            content = content.replace(char, replacement)
        
        # Replace any remaining non-ASCII characters with their Unicode escape sequence
        def replace_non_ascii(match):
            char = match.group(0)
            return f"[U+{ord(char):04X}]"
        
        content = re.sub(r'[^\x00-\x7F]', replace_non_ascii, content)
        
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(content)
        
        print(f"Fixed file saved to {output_path}")
        return True
    except Exception as e:
        print(f"Error fixing {file_path}: {e}")
        return False

if __name__ == "__main__":
    input_file = "app.py"
    output_file = "app_fixed.py"
    
    print(f"Checking {input_file} for Unicode characters...")
    find_unicode_chars(input_file)
    
    print(f"\nReplacing Unicode characters in {input_file}...")
    replace_unicode_chars(input_file, output_file)