10 lines
440 B
Python
10 lines
440 B
Python
"""Rewrite .py files so they are pure ASCII (non-ASCII characters become \\u / \\U escapes)."""
|
|||
|
|
import glob, sys
|
||
|
|
for f in sys.argv[1:] or glob.glob("*.py"):
|
||
|
|
s = open(f, encoding="utf-8").read()
|
||
|
|
if all(ord(c) < 128 for c in s):
|
||
|
|
continue
|
||
|
|
out = "".join(c if ord(c) < 128 else ("\\u%04x" % ord(c) if ord(c) <= 0xFFFF else "\\U%08x" % ord(c)) for c in s)
|
||
|
|
open(f, "w", encoding="ascii").write(out)
|
||
|
|
print("escaped", f)
|