473,320 Members | 1,965 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,320 software developers and data experts.

SGMLParser eats ä etc

Hello!

I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
with letters that has been coded, e.g. the letter å is coded å ä is
coded ä and ö is coded ö all according to the html standard.

I use the SGMLParser and when I feed method all the coded letter will be
stripped/eaten.

Why?
How do I fix this?

// Anders
Jul 18 '05 #1
8 2445
Anders Eriksson <am******@telia.com> writes:
I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
with letters that has been coded, e.g. the letter å is coded &aring; ä is
coded &auml; and ö is coded &ouml; all according to the html standard.

I use the SGMLParser and when I feed method all the coded letter will be
stripped/eaten.

Why?
How do I fix this?


You probably want to use HTMLParser.HTMLParser instead (NOT the same
thing as htmllib.HTMLParser, note). It knows about XHTML, sgmllib &
htmllib don't. If you really want sgmllib, though (untested):

import htmlentitydefs

class MyParser(sgmllib.SGMLParser):
entitydefs = htmlentitydefs.entitydefs

def unknown_entityref(self, ref):
...

...
John
Jul 18 '05 #2
Anders Eriksson wrote:
Hello!

I'm using smgllib (ActivePython 2.3.2, build 230) and I have some trouble
with letters that has been coded, e.g. the letter å is coded &aring; ä is
coded &auml; and ö is coded &ouml; all according to the html standard.

I use the SGMLParser and when I feed method all the coded letter will be
stripped/eaten.

Why?
How do I fix this?


The &something; "coding" for accented characters is called an entity in SGML.
These entities are all defined in the underlying DTD for your document. HTML
defines the "standard" entities you describe, like &aring;, &auml;, etc... But
if the DTD for the document you're parsing does not include these entity
definitions, there's no reason why the parser should do anything with them, even
if silently ignoring them seems strange to me (I'd have expected a parsing error).

So there are two solutions:
- either your document is HTML, and you should use an HTML parser as it was
already suggested
- or your document is not HTML, and you should define all entities you may use
in your DTD. This is done for example with:
<!ENTITY auml ä>
(if you use the iso8859-1 encoding)

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #3
Eric Brunel <er*********@N0SP4M.com> writes:
Anders Eriksson wrote:
Hello!
I'm using smgllib (ActivePython 2.3.2, build 230) and I have some
[...] So there are two solutions:
- either your document is HTML, and you should use an HTML parser as
it was already suggested
- or your document is not HTML, and you should define all entities you
may use in your DTD. This is done for example with:

[...]

sgmllib only really does HTML.
John
Jul 18 '05 #4
On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
You probably want to use HTMLParser.HTMLParser instead (NOT the same
thing as htmllib.HTMLParser, note). It knows about XHTML, sgmllib &
htmllib don't.

&aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.

the strange thing is that the Character entity (i.e. &aring;) is stripped
from the text. I don't want to change it since I'm feeding the output to a
browser.

I will try the HTMLParser instead but it seems to me that there is a bug in
SMGLParser...

// Anders
Jul 18 '05 #5
Anders Eriksson wrote:

On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
You probably want to use HTMLParser.HTMLParser instead (NOT the same
thing as htmllib.HTMLParser, note). It knows about XHTML, sgmllib &
htmllib don't.

&aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.

the strange thing is that the Character entity (i.e. &aring;) is stripped
from the text. I don't want to change it since I'm feeding the output to a
browser.

I will try the HTMLParser instead but it seems to me that there is a bug in
SMGLParser...


If it's anything like the expat parser, it munches any undefined character
entity references (i.e. if there's a DTD but no definitions) unless you plug
in an appropriate entity reference subparser.

-Peter
Jul 18 '05 #6
Anders Eriksson <am******@telia.com> writes:
On 30 Nov 2003 00:53:28 +0000, John J. Lee wrote:
You probably want to use HTMLParser.HTMLParser instead (NOT the same
thing as htmllib.HTMLParser, note). It knows about XHTML, sgmllib &
htmllib don't. &aring; etc isn't XHTML, is it? AFAIK it is defined in HTML 4.


It'll cope with HTML too. It seems silly to be writing new code now
that will choke on XHTML.

the strange thing is that the Character entity (i.e. &aring;) is stripped
from the text. I don't want to change it since I'm feeding the output to a
browser.
Did you read my post? Read the docs on the stuff in my code snippet.

I will try the HTMLParser instead but it seems to me that there is a bug in
SMGLParser...


It's not a bug. That's just what it does, but you can easily override it.
John
Jul 18 '05 #7
Anders Eriksson <am******@telia.com> writes:
the strange thing is that the Character entity (i.e. &aring;) is
stripped from the text. I don't want to change it since I'm feeding
the output to a browser.
Inconvenient for you, but not strange. An SGML parser is supposed to
expand general entity references.
I will try the HTMLParser instead but it seems to me that there is a
bug in SMGLParser...


No, it's consistent with the standard that the entity reference
disappears. The question is what replacement text has been put in its
place, and why can't you see it?

Dave Dubin

Jul 18 '05 #8
ddubin <dd****@lindev.isrl.uiuc.edu> writes:
Anders Eriksson <am******@telia.com> writes: [...] disappears. The question is what replacement text has been put in its
place, and why can't you see it?


He can't see it because it's not there. From sgmllib.py:

# To be overridden -- handlers for unknown objects
def unknown_starttag(self, tag, attrs): pass
def unknown_endtag(self, tag): pass
def unknown_charref(self, ref): pass
def unknown_entityref(self, ref): pass
John
Jul 18 '05 #9

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

Similar topics

6
by: KhanyBoy | last post by:
Hi, this should test you guru's. I want a function that accepts text as an argument and converts all & into &amp; except where it is a html character already such as &nbsp;, &quot;, and of course &amp;. ...
13
by: Robert Zierhofer | last post by:
Hi all, I currently face a problem with htmlentities and german "umlaute". After moving my scripts to a new box (from Linux to FreeBSD) I had to see that htmlentities is not working anymore....
2
by: ahsan Imam | last post by:
Hello All, I have this file and when I import the file in the python interpretor I get the following error: "__main__:1: DeprecationWarning: Non-ASCII character '\xc0' in file trans.py on...
9
by: Anna | last post by:
Hi, I use the Swedish letters å,ä,ö in my HTML code and it works perfectly in IE, but not in Netscape and Mozilla browsers. Does anyone know how to fix it? I use &aring for å, &auml for ä and...
8
by: Colin Peters | last post by:
Hi, I'm reading a file and writing it to the html output for a page. I've come across two difficulties which I would like to solve. The files contain special characters from European...
2
by: Stephen Brown | last post by:
I have a simple regex need and I've already wasted too much time on it spinning in circles. Can a regex god help a stranded soul? I just need to replace all non-escaped ampersands in a file. It...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
3
by: John Nagle | last post by:
I have XML replies in a DOM which contain entity escapes, like "&amp;". What's the proper way to replace them with the ordinary characters? Preferably something that will work in most browsers? I...
3
by: yuanyun.ken | last post by:
hi,dear all js gurus. In my app, server responses some text like: '&nbsp;&nbsp; ' and I need display these content in textarea. But Html would convert specail characters to space, and ignore line...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.