def to_ascii(s): """Map string to ASCII""" import unicode2ascii if not s: return s if to_ascii.mapping is None: mapfn = os.path.join(os.path.dirname(__file__), 'entities.dat') with codecs.open(mapfn, encoding='utf-8') as f: to_ascii.mapping = unicode2ascii.read_mapping(f, mapfn) out = StringIO() unicode2ascii.process([s], out, to_ascii.mapping) return out.getvalue()
def to_ascii(s): """Map string to ASCII""" import unicode2ascii if not s: return s, 0 if to_ascii.mapping is None: mapfn = os.path.join(os.path.dirname(__file__), 'entities.dat') with open(mapfn, encoding='utf-8') as f: to_ascii.mapping = unicode2ascii.read_mapping(f, mapfn) out = StringIO() missing_count = unicode2ascii.process([s], out, to_ascii.mapping) return out.getvalue(), missing_count