473,791 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ 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 unicode2htmlent ities(u):

htmlentities = list()

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

return ''.join(htmlent ities)

print unicode2htmlent ities(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 4707

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

news:11******** *************@n 15g2000prd.goog legroups.com...
I was looking for a function to transform a unicode string into
htmlentities.
>u'São Paulo'.encode(' ascii', 'xmlcharrefrepl ace')

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

Thanks, Clodoaldo

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

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

'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', 'xmlcharrefrepl ace')
'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 unicode2htmlent ities(u):

htmlentities = list()

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

return ''.join(htmlent ities)

print unicode2htmlent ities(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 unicode2htmlent ities(u):
htmlentities = list()
for c in u:
if ord(c) < 128:
htmlentities.ap pend(c)
else:
htmlentities.ap pend('&%s;' % codepoint2name[ord(c)])
return ''.join(htmlent ities)
print unicode2htmlent ities(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úvida s>Mail 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...@i nvalid.invalidw rote:
Clodoaldo <clodoaldo.pi.. .@gmail.comwrot e:
On May 29, 12:57 pm, "Richard Brodie" <R.Bro...@rl.ac .ukwrote:
"Clodoaldo" <clodoaldo.pi.. .@gmail.comwrot e in message
>news:11******* **************@ n15g2000prd.goo glegroups.com.. .
I was looking for a function to transform a unicode string into
htmlentities.
u'São Paulo'.encode(' ascii', 'xmlcharrefrepl ace')
'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', 'xmlcharrefrepl ace')

'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
4418
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+ browsers that were tested and those that only work in some of them. Take a look, maybe you'll consider it useful. This is not my field (I'm an architect - you know the house construction kind), so if you notice any inaccuracies I'd appreciate a...
3
2687
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 unicode doesn't display outside script tags: a\xb2, a\xb3, a\u2074
2
12980
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 entities. eg characters on this site: http://unicode.e-workers.de/kyrillisch.php come back as eg Л . i'm not shure, where exactly this happens. it doesn't happen on ASP.NET sites though. the top of those documents looks
1
3209
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 textbox when the web browser is closed. The only problem comes in the use of numeric entities to specify Unicode. The web browser control is fine when it comes to named entities, like &nbsp; . And there are a lot of named entities, just...
1
2108
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? TIA Zunbeltz
8
2812
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 available function to solve this issue? As an example I would like to do this kind of conversion: \uc3B4 =&ocirc; for all available html entities. thanks,
2
2804
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 program sorts out every doublet and the hexadecimal unicode code is extracted, but I dont know a way to find the xml or sgml-entity equivalent to the unicode code. Anyone who could give me a pointer? Best regards
3
8748
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 mailbody are stored as Unicode entities (eg. 註)
8
19857
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 acceptable:
2
4884
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 into web page n it shows special charactor easily. Here is the function code function charset_decode_utf_8($string) { /* Only do the slow convert if there are 8-bit characters */ /* avoid using 0xA0 (\240) in ereg ranges. RH73 does not...
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.