Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Replace UTF-8 "extended" characters with ASCII equivalents
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import io
Input file might look something like this:
cat input.txt
some ñ thing
foo ñññ
When the script is done, the output.txt will look like this:
cat output.txt
some x thing
foo xxx
There are other libraries that will do this automagically (e.g. unidecode), but in my case I wanted control over what gets
mapped to what.
replacement_map = {
ord(u'ñ'): u'x',
# ... other mappings here...
with'input.txt', encoding='utf-8') as data:
with'output.txt', 'w', encoding='ascii') as out:
for row in data:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.