what module can do html encoder??

R

richard

Leon said:
example:
s = ' ' --->  

That's technically not HTML encoding, that's replacing a perfectly valid
space character with a *non-breaking* space character. If that's all you
want to do, then:

s.replace(' ', ' ')

will do it. If, however, you wish to quote special HTML chars, like <, > and
&, then:

cgi.escape(s)

will do that for you. From the library reference for "escape(
s[, quote])":

Convert the characters "&", "<" and ">" in string s to HTML-safe
sequences. Use this if you need to display text that might contain such
characters in HTML. If the optional flag quote is true, the double-quote
character (""") is also translated; this helps for inclusion in an HTML
attribute value, as in <A HREF="...">.


Richard
 
G

Gerrit

richard said:
That's technically not HTML encoding, that's replacing a perfectly valid
space character with a *non-breaking* space character.

How can you tell?

s = ' ' # non-breaking space
s = ' ' # normal space
s = 'á¿Ÿ' # em-space

But you might want to do something like:

def escapechar(s):
import htmlentitydefs
n = ord(s)
if n < 128:
return s.encode('ascii')
elif n in htmlentitydefs.codepoint2name:
return '&%s;' % htmlentitydefs.codepoint2name[n]
else:
return '&#%d;' % ord(s)

This requires unicode strings, because unicode encodings have multi-byte
characters. Demonstration:
's'

yours,
Gerrit Holl.

--
Weather in Lulea / Kallax, Sweden 13/12 10:20:
-15.0°C wind 0.9 m/s NNW (34 m above NAP)
--
In the councils of government, we must guard against the acquisition of
unwarranted influence, whether sought or unsought, by the
military-industrial complex. The potential for the disastrous rise of
misplaced power exists and will persist.
-Dwight David Eisenhower, January 17, 1961
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,111
Members
47,703
Latest member
robert.marryson

Latest Threads

Top