473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unicode to HTML entities

I was looking for a function to transform a unicode string into
htmlentities. Not only the usual html escaping thing but all
characters.

As I didn't find I wrote my own:

# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name

def unicode2htmlentities(u):

htmlentities = list()

for c in u:
if ord(c) < 128:
htmlentities.append(c)
else:
htmlentities.append('&%s;' % codepoint2name[ord(c)])

return ''.join(htmlentities)

print unicode2htmlentities(u'São Paulo')

Is there a function like that in one of python builtin modules? If not
is there a better way to do it?

Regards, Clodoaldo Pinto Neto

May 29 '07 #1
6 4673

"Clodoaldo" <cl*************@gmail.comwrote in message
news:11*********************@n15g2000prd.googlegro ups.com...
>I was looking for a function to transform a unicode string into
htmlentities.
>>u'São Paulo'.encode('ascii', 'xmlcharrefreplace')
'São Paulo'
May 29 '07 #2
On May 29, 12:57 pm, "Richard Brodie" <R.Bro...@rl.ac.ukwrote:
"Clodoaldo" <clodoaldo.pi...@gmail.comwrote in message

news:11*********************@n15g2000prd.googlegro ups.com...
I was looking for a function to transform a unicode string into
htmlentities.
>u'São Paulo'.encode('ascii', 'xmlcharrefreplace')

'São Paulo'
That was a fast answer. I would never find that myself.

Thanks, Clodoaldo

May 29 '07 #3
Clodoaldo <cl*************@gmail.comwrote:
On May 29, 12:57 pm, "Richard Brodie" <R.Bro...@rl.ac.ukwrote:
>"Clodoaldo" <clodoaldo.pi...@gmail.comwrote in message

news:11*********************@n15g2000prd.googlegr oups.com...
>I was looking for a function to transform a unicode string into
htmlentities.
u'São Paulo'.encode('ascii', 'xmlcharrefreplace')

'São Paulo'

That was a fast answer. I would never find that myself.
You might actually want:
>>cgi.escape(u'São Paulo & Espírito Santo').encode('ascii', 'xmlcharrefreplace')
'São Paulo &amp; Espírito Santo'

as you have to be sure to escape any ampersands in your unicode
string before doing the encode.

May 30 '07 #4

On 29 maj 2007, at 17.52, Clodoaldo wrote:
I was looking for a function to transform a unicode string into
htmlentities. Not only the usual html escaping thing but all
characters.

As I didn't find I wrote my own:

# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name

def unicode2htmlentities(u):

htmlentities = list()

for c in u:
if ord(c) < 128:
htmlentities.append(c)
else:
htmlentities.append('&%s;' % codepoint2name[ord(c)])

return ''.join(htmlentities)

print unicode2htmlentities(u'São Paulo')

Is there a function like that in one of python builtin modules? If not
is there a better way to do it?

Regards, Clodoaldo Pinto Neto
In many cases, the need to use html/xhtml entities can be avoided by
generating
utf8- coded pages.
------------------------------------------------------
"Home is not where you are born, but where your heart finds peace" -
Tommy Nordgren, "The dying old crone"
to************@comhem.se
May 30 '07 #5
On May 30, 8:53 am, Tommy Nordgren <tommy.nordg...@comhem.sewrote:
On 29 maj 2007, at 17.52, Clodoaldo wrote:
I was looking for a function to transform a unicode string into
htmlentities. Not only the usual html escaping thing but all
characters.
As I didn't find I wrote my own:
# -*- coding: utf-8 -*-
from htmlentitydefs import codepoint2name
def unicode2htmlentities(u):
htmlentities = list()
for c in u:
if ord(c) < 128:
htmlentities.append(c)
else:
htmlentities.append('&%s;' % codepoint2name[ord(c)])
return ''.join(htmlentities)
print unicode2htmlentities(u'São Paulo')
Is there a function like that in one of python builtin modules? If not
is there a better way to do it?
Regards, Clodoaldo Pinto Neto

In many cases, the need to use html/xhtml entities can be avoidedby
generating
utf8- coded pages.
Sure. All my pages are utf-8 encoded. The case I'm dealing with is an
email link which subject has non ascii characters like in:

<a href=mailto:ex*****@sample.com?subject=Dúvidas>Mai l to</a>

Somehow when the user clicks on the link the subject goes to his email
client with the non ascii chars as garbage.

And before someone points that I should not expose email addresses,
the email is only linked with the consent of the owner and the source
is obfuscated to make it harder for a robot to harvest it.

Regards, Clodoaldo

May 30 '07 #6
On May 30, 4:25 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Clodoaldo <clodoaldo.pi...@gmail.comwrote:
On May 29, 12:57 pm, "Richard Brodie" <R.Bro...@rl.ac.ukwrote:
"Clodoaldo" <clodoaldo.pi...@gmail.comwrote in message
>news:11*********************@n15g2000prd.googlegr oups.com...
I was looking for a function to transform a unicode string into
htmlentities.
u'São Paulo'.encode('ascii', 'xmlcharrefreplace')
'São Paulo'
That was a fast answer. I would never find that myself.

You might actually want:
>cgi.escape(u'São Paulo & Espírito Santo').encode('ascii', 'xmlcharrefreplace')

'São Paulo &amp; Espírito Santo'

as you have to be sure to escape any ampersands in your unicode
string before doing the encode.
I will do it. Thanks.

Regards, Clodoaldo.

May 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4405
by: Nancy | last post by:
I recently completed a web page, "Browser Tests of Entities in 2004". http://www.santagata.us/characters/CharacterEntities.html It shows those characters that work in all of the version 5.2+...
3
2659
by: dalei | last post by:
My question is presented more clearly in following web page: http://www.pinyinology.com/signs2.html <html> HTML entities display outside script tags: a&sup1;, a&sup2;, a&sup3;, a⁴ But...
2
12912
by: Beat Richli | last post by:
Hello i have following problem with ASP (using Interdev, Win2003 Server): if a special character is entered in a textbox, ASP or the Client Browser (IE 6) seems to convert this character in HTML...
1
3190
by: Mark Johnson | last post by:
I wonder if anyone has a solution? I wanted to use the web browser control as a 'zoom' box for a smaller textbox. I can format in the control, and save whatever formatting as HTML code back to the...
1
2086
by: zunbeltz | last post by:
Hi, I'm parsing html. I have a page with a lot of html enitties for hebrew characters. When i print what i get are blanks, dots and commas. How can i decode this entities to unicode charachters?...
8
2788
by: lorenzo.viscanti | last post by:
X-No-Archive: yes Hi, I've found lots of material on the net about unicode html conversions, but still i'm having many problems converting unicode characters to html entities. Is there any...
2
2787
by: Frantic | last post by:
I'm working on a list of japaneese entities that contain the entity, the unicode hexadecimal code and the xml/sgml entity used for that entity. A unicode document is read into the program, then the...
3
8706
by: Laangen_LU | last post by:
Dear Group, my first post to this group, so if I'm on the wrong group, my apologies. I'm trying to send out an email in Chinese lanuage using the mail() function in PHP. Subject and...
8
19785
by: Steven D'Aprano | last post by:
I have a string containing Latin-1 characters: s = u"© and many more..." I want to convert it to HTML entities: result => "&copy; and many more..." Decimal/hex escapes would be...
2
4855
by: neovantage | last post by:
hey geeks, I am using a function which convert unicode to entities. So that i can save values into mysql database into entities. This function really helps me when i display the store entity data...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.