Least-lossy string.encode to us-ascii?

Terry Reedy tjreedy at udel.edu
Fri Sep 14 16:57:29 EDT 2012


On 9/13/2012 10:09 PM, Mark Tolonen wrote:
> On Thursday, September 13, 2012 4:53:13 PM UTC-7, Tim Chase wrote:
>> On 09/13/12 18:36, Terry Reedy wrote:

>>> 'keep as much information as possible' would mean an effectively
>>> lossless transliteration, which you could do with a dict.
>>> {<o-with-accent>: 'o', <c-cedilla>: 'c,' (or pick something that would

>> Vlastimil's solution kept the characters but stripped them of their
>> accents/tildes/cedillas/etc, doing just what I wanted, all using the
>> stdlib.  Hard to do better than that :-)

You mean, hard to do better than what you think you want, as opposed to 
what you said you wanted in both the subject line and the text line I 
quoted. What you need depends on why you need ascii only text and what 
the recipient will do with the ascii only text. Print it on an 
ascii-only printer? Or something similar? If so, a lossy encoding may be 
sufficient, but why not let the recipient decide to toss info?

> How about using UTF-7 for transmission and decode on the other end?
 > This keeps the transmission all 7-bit, and no loss.
>
>      >>> s=u"serviço móvil".encode('utf-7')
>      >>> print s
>      servi+AOc-o m+APM-vil
>      >>> print s.decode('utf-7')
>      serviço móvil

Nice. I was barely aware of and forgot that option. This and similar 
suggestions to use existing methods is much better than my hackish approach.

-- 
Terry Jan Reedy





More information about the Python-list mailing list