Skip to content

Instantly share code, notes, and snippets.

@oseiskar
Created October 7, 2015 16:09
Show Gist options
  • Save oseiskar/59dd6fc6723060197245 to your computer and use it in GitHub Desktop.
Save oseiskar/59dd6fc6723060197245 to your computer and use it in GitHub Desktop.
Finds and highlights non-ASCII characters in STDIN, line-by-line
#!/usr/bin/python
"Finds and highlights non-ASCII characters in STDIN (python 2)"
import sys
def is_ascii(str):
try:
str.encode('ascii')
return True
except: return False
line_num = 0
for line in sys.stdin:
line_num += 1
if is_ascii(line): continue
sys.stdout.write("\nline %d:\n" % line_num)
underline = ''
for char in line:
if is_ascii(char):
underline += ' '
else:
char = '?'
underline += '^'
sys.stdout.write(char)
sys.stdout.write(underline + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment