|
""" |
|
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() |
|
|
|
|
|
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() |
|
|
|
|
|
replacements = { |
|
'✓': '[PASS]', |
|
'✗': '[FAIL]', |
|
'❌': '[ERROR]', |
|
'✅': '[SUCCESS]', |
|
'⚠️': '[WARNING]', |
|
'→': '->', |
|
'←': '<-', |
|
'…': '...', |
|
'–': '-', |
|
'—': '--', |
|
''': "'", |
|
''': "'", |
|
'"': '"', |
|
'"': '"', |
|
'•': '*', |
|
'©': '(c)', |
|
'®': '(R)', |
|
'™': '(TM)', |
|
'°': ' degrees', |
|
'±': '+/-', |
|
'×': 'x', |
|
'÷': '/', |
|
'≤': '<=', |
|
'≥': '>=' |
|
} |
|
|
|
for char, replacement in replacements.items(): |
|
content = content.replace(char, replacement) |
|
|
|
|
|
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) |