Least-lossy string.encode to us-ascii?

wxjmfauth at gmail.com wxjmfauth at gmail.com
Fri Sep 14 12:15:25 EDT 2012


Le jeudi 13 septembre 2012 23:25:27 UTC+2, Tim Chase a écrit :
> I've got a bunch of text in Portuguese and to transmit them, need to
> 
> have them in us-ascii (7-bit).  I'd like to keep as much information
> 
> as possible, just stripping accents, cedillas, tildes, etc.  So
> 
> "serviço móvil" becomes "servico movil".  Is there anything stock
> 
> that I've missed?  I can do mystring.encode('us-ascii', 'replace')
> 
> but that doesn't keep as much information as I'd hope.
> 

Interesting case. It's where the coding of characters
meets characters usage, scripts, typography, linguistic
features.

I cann't discuss the Portugese case, but in French
and in German one way to achieve the task is to
convert the text in uppercases. It preserves a correct
text.

>>> s = 'Lætitia  cœur  éléphant  français  LUŸ  Stoß  Erklärung  stören'
>>> libfrancais.SpecMajuscules(s)
'LAETITIA  COEUR  ELEPHANT  FRANCAIS  LUY  STOSS  ERKLAERUNG 
STOEREN'

>>> r = 'LAETITIA  COEUR  ELEPHANT  FRANCAIS  LUY  STOSS  ERKLAERUNG STOEREN'
>>> r.encode('ascii', 'strict').decode('ascii', 'strict') == r
True

PS Avoid Py3.3 :-)

jmf




More information about the Python-list mailing list