k4ever/client-barcode/freitagslib/encoding.py

36 lines
754 B
Python
Raw Normal View History

#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
# Licensed under GPL v3 or later
from __future__ import print_function
def asciify(utf8):
_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 = utf8.decode('utf-8')
unitext = ''.join(subst(e) for e in unichars)
return unitext.encode('ascii')
if __name__ == '__main__':
print(asciify('Schöfferhofer'))