Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19699

Re: xhtml encoding question

From Peter Otten <__peter__@web.de>
Subject Re: xhtml encoding question
Date 2012-02-01 10:32 +0100
Organization None
References <jg9apg$v0$1@foggy.unx.sas.com> <daanv8-7i.ln1@satorlaser.homedns.org>
Newsgroups comp.lang.python
Message-ID <mailman.5292.1328088791.27778.python-list@python.org> (permalink)

Show all headers | View raw


Ulrich Eckhardt wrote:

> Am 31.01.2012 19:09, schrieb Tim Arnold:
>> high_chars = {
>>     0x2014:'&mdash;', # 'EM DASH',
>>     0x2013:'&ndash;', # 'EN DASH',
>>     0x0160:'&Scaron;',# 'LATIN CAPITAL LETTER S WITH CARON',
>>     0x201d:'&rdquo;', # 'RIGHT DOUBLE QUOTATION MARK',
>>     0x201c:'&ldquo;', # 'LEFT DOUBLE QUOTATION MARK',
>>     0x2019:"&rsquo;", # 'RIGHT SINGLE QUOTATION MARK',
>>     0x2018:"&lsquo;", # 'LEFT SINGLE QUOTATION MARK',
>>     0x2122:'&trade;', # 'TRADE MARK SIGN',
>>     0x00A9:'&copy;', # 'COPYRIGHT SYMBOL',
>> }
> 
> You could use Unicode string literals directly instead of using the
> codepoint, making it a bit more self-documenting and saving you the
> later call to ord():
> 
> high_chars = {
>      u'\u2014': '&mdash;',
>      u'\u2013': '&ndash;',
>      ...
> }
> 
>> for c in string:
>>     if ord(c) in high_chars:
>>         c = high_chars.get(ord(c))
>>     s += c
>> return s
> 
> Instead of checking if there is a replacement and then looking up the
> replacement again, just use the default:
> 
>    for c in string:
>        s += high_chars.get(c, c)
> 
> Alternatively, if you find that clearer, you could also check if the
> returnvalue of get() is None to find out if there is a replacement:
> 
>    for c in string:
>        r = high_chars.get(c)
>        if r is None:
>            s += c
>        else:
>            s += r

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"&copy;", 0x2122: u"&trade;"})
u'&copy;&trade;'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

xhtml encoding question Tim Arnold <Tim.Arnold@sas.com> - 2012-01-31 13:09 -0500
  Re: xhtml encoding question Stefan Behnel <stefan_ml@behnel.de> - 2012-02-01 09:26 +0100
    Re: xhtml encoding question Tim Arnold <Tim.Arnold@sas.com> - 2012-02-01 13:15 -0500
      Re: xhtml encoding question Stefan Behnel <stefan_ml@behnel.de> - 2012-02-02 08:02 +0100
  Re: xhtml encoding question Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-02-01 09:39 +0100
    Re: xhtml encoding question Peter Otten <__peter__@web.de> - 2012-02-01 10:32 +0100
      Re: xhtml encoding question Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-02-01 17:03 +0100
        Re: xhtml encoding question Peter Otten <__peter__@web.de> - 2012-02-02 12:02 +0100
          Re: xhtml encoding question Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-02-02 13:40 +0100

csiph-web