Add a method to convert str to ascii in Python 3

This commit is contained in:
nicolargo 2017-12-16 15:05:06 +01:00
parent 1a4219b208
commit 54b61fd5a0

View File

@ -64,7 +64,9 @@ if PY3:
def to_ascii(s): def to_ascii(s):
"""Convert the bytes string to a ASCII string """Convert the bytes string to a ASCII string
Usefull to remove accent (diacritics)""" Usefull to remove accent (diacritics)"""
return str(s, 'utf-8') if isinstance(s, binary_type):
return s
return s.encode('ascii', 'ignore')
def listitems(d): def listitems(d):
return list(d.items()) return list(d.items())
@ -124,7 +126,7 @@ else:
Usefull to remove accent (diacritics)""" Usefull to remove accent (diacritics)"""
if isinstance(s, binary_type): if isinstance(s, binary_type):
return s return s
return unicodedata.normalize('NFKD', s).encode('ASCII', 'ignore') return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
def listitems(d): def listitems(d):
return d.items() return d.items()