xhtml encoding question

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Wed Feb 1 17:03:19 CET 2012


Am 01.02.2012 10:32, schrieb Peter Otten:
> It doesn't matter for the OP (see Stefan Behnel's post), but If you want to
> replace characters in a unicode string the best way is probably the
> translate() method:
>
>>>> print u"\xa9\u2122"
> ©™
>>>> u"\xa9\u2122".translate({0xa9: u"©", 0x2122: u"™"})
> u'©™'
>

Yes, this is both more expressive and at the same time probably even 
more efficient.


Question though:

 >>> u'abc'.translate({u'a': u'A'})
u'abc'

I would call this a chance to improve Python. According to the 
documentation, using a string is invalid, but it neither raises an 
exception nor does it do the obvious and accept single-character strings 
as keys.


Thoughts?


Uli



More information about the Python-list mailing list