#! /usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) 2011 Sebastian Pipping # Licensed under GPL v3 or later from __future__ import print_function def ensure_unicode(candidate, encoding='utf-8'): if isinstance(candidate, basestring) \ and not isinstance(candidate, unicode): return unicode(candidate, encoding) return candidate def asciify(text): _mapping = ( ('ö', 'oe'), ('Ö', 'Oe'), ('ä', 'ae'), ('Ä', 'Ae'), ('ü', 'ue'), ('Ü', 'Ue'), ('ß', 'ss'), ) _mapping = dict(((ord(unicode(key, 'utf-8')), value) for key, value in _mapping)) def subst(unichar): def fix(unichar): if ord(unichar) > 127: return unicode('?') return unichar return _mapping.get(ord(unichar), fix(unichar)) unichars = ensure_unicode(text, 'utf-8') unitext = ''.join(subst(e) for e in unichars) return unitext.encode('ascii') if __name__ == '__main__': print(asciify('Schöfferhofer'))