-
-
Save sente/806610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
def unicode_safe_127(ss, debug=False): | |
"""safely handle strings with have ordinal values greater than 127 | |
by escaping each value as appropriate. | |
if debug=True, log each conversion to stderr | |
""" | |
mys = ss | |
tos=[] | |
for i,s in enumerate(mys): | |
if ord(s)>127: | |
if debug: | |
sys.stderr.write(codecs.unicode_escape_encode(mys)[0] + "\n") | |
tos.append(codecs.unicode_escape_encode(s)[0]) | |
else: | |
tos.append(s) | |
return "".join(tos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment