473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4665

"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
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.