fork download
  1. #!/usr/bin/env python3
  2.  
  3. from unicodedata import normalize
  4.  
  5.  
  6. def latinize(string):
  7. """
  8. Map string to Latin-1, replacing characters which can be approximated
  9. """
  10. result = []
  11. for char in string:
  12. try:
  13. byte =normalize("NFKC", char).encode('latin-1')
  14. except UnicodeEncodeError:
  15. byte = normalize("NFKD", char).encode('ascii', 'ignore')
  16. result.append(byte)
  17. return b''.join(result)
  18.  
  19. def convert(fh):
  20. for line in fh:
  21. print(latinize(line), end='')
  22.  
  23. def main():
  24. import sys
  25. if len(sys.argv) > 1:
  26. for filename in sys.argv[1:]:
  27. with open(filename, 'r') as fh:
  28. convert(fh)
  29. else:
  30. convert(sys.stdin)
  31.  
  32. if __name__ == '__main__':
  33. main()
Success #stdin #stdout 0.02s 9412KB
stdin
måy yöür ďřěǎmŝ ćőmé ťřűë
stdout
b'm\xe5y y\xf6\xfcr dreams com\xe9 tru\xeb'