else/Safeprint/safeprint.py
Ethan Dalool b5a1649bc0 else
2017-11-03 12:50:16 -07:00

18 lines
613 B
Python

'''
This function is slow and ugly, but I need a way to safely print unicode strings
on systems that don't support it without crippling those who do.
'''
def safeprint(text, file_handle=None, end='\n'):
for character in text:
try:
if file_handle:
file_handle.write(character)
else:
print(character, end='', flush=False)
except UnicodeError:
if file_handle:
file_handle.write('?')
else:
print('?', end='', flush=False)
if not file_handle:
print(end, end='', flush=True)