473,387 Members | 3,787 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,387 software developers and data experts.

Help with regular expressions

I have a problem. I have written a python based theme for a linux app
called superkaramba, which is effectively an engine for desktop applets
that utilises python as its theming language. The script basically parses
weather websites, and displays the info in a visually appealing way.

A couple of other people have contributed code to this project,
particularly relating to the parsing of the websites. Unfortunately, it
is not parsing one particular part of the website properly. This is
because it is expecting the data to be in a certain form, and occasionally
it is in a different form. Unfortunately this causes the entire script to
fail to run.

Unfortunately, I know very little about regular expressions and can't get
hold of the person who wrote this part of the script. The other issue I am
struggling with is that there are no error messages as to what's going
wrong, which makes it more difficult to code round the issue.

The issue comes down to a couple of lines in the html for the web page.
The following lines parse correctly:

<TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo1>Wind:</TD>
<TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo2>From the Northeast at 6&nbsp;mph</TD>

these don't:

<TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo1>Wind:</TD>
<TD ALIGN=LEFT VALIGN=TOP CLASS=obsInfo2>calm&nbsp;</TD>

the relevant portion of the python script is as follows:

print '================================================= =='
p_current = r'''(?isx) # Ignore case, Dot matches all, Verbose
wxicons/52/(?P<icon>\d*?)\.gif # Icon
..*?obsTempTextA>(?P<temp>\d*?)&deg; # Temp
..*?obsTextA>(?P<sky>.*?)</b> # Sky
..*?Feels\sLike<br>(?P<heat>.*?)&deg; # Heat
..*?UV\sIndex:.*?Info2>(?P<uv>.*?)&nbsp
..*?Dew\sPoint:.*?Info2>(?P<dew>.*?)&deg;
..*?Humidity:.*?Info2>(?P<hum>\d+)
..*?Visibility:.+?Info2>(?P<vis>.*?)</td>
..*?Pressure:.+?Info2>(?P<baro>.*?)\sinches\sand\s (?P<change>.*?)</td>
..*?Wind:.+?Info2>(?P<wind>.*?)\sat\s(?P<speed>\d* ?)&nbsp;
'''
match = re.search(p_current, data1)
if match:
now.icon(match.group('icon'))
now.temperature(match.group('temp'), 'F')
now.relative_heat(match.group('heat'), 'F')
now.sky(match.group('sky'))
now.uv(match.group('uv'))
now.dewpoint(match.group('dew'), 'F')
now.humidity(match.group('dew'))
now.visibility(match.group('vis'))
now.pressure(match.group('baro'), 'inHg')
now.pressure_change(match.group('change'))
mywind = match.group('wind')
now.wind(mywind.replace('From the ', ''))
now.wind_speed(match.group('speed'), 'mph')

Obviously the issue is that the regular expression expects "at", and in
the second line of the html that doesn't parse, there is no at.

The question I have, is how do I go about fixing this. What I want is to
test to see if the line does or doesn't contain an "at", and if not,
change it to contain an "at". I'm just not sure how to code the RE in
python to do this.

Any help would be appreciated.

Matt
Jul 18 '05 #1
3 1824
dmbkiwi enlightened us with:
A couple of other people have contributed code to this project,
particularly relating to the parsing of the websites.
Unfortunately, it is not parsing one particular part of the website
properly. This is because it is expecting the data to be in a
certain form, and occasionally it is in a different form.
Unfortunately this causes the entire script to fail to run.


You seem to expect old HTML. Why not use XHTML only ('tidy' can
convert between them) and use a regular XML parser? Much, much, much
easier! And you won't have to be afraid of messing up your regular
expressions ;-)

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Jul 18 '05 #2
On Tue, 26 Aug 2003 08:47:33 +0000, Sybren Stuvel wrote:
dmbkiwi enlightened us with:
A couple of other people have contributed code to this project,
particularly relating to the parsing of the websites.
Unfortunately, it is not parsing one particular part of the website
properly. This is because it is expecting the data to be in a
certain form, and occasionally it is in a different form.
Unfortunately this causes the entire script to fail to run.


You seem to expect old HTML. Why not use XHTML only ('tidy' can
convert between them) and use a regular XML parser? Much, much, much
easier! And you won't have to be afraid of messing up your regular
expressions ;-)

Sybren


XML would be nice, but unfortunately I have no choice as to the markup
language used by the site. It's a website on the world wide web, not a
site overwhich I have any control. My regular expressions are at the
mercy of the developers of that site.

Any other suggestions?

Matt
Jul 18 '05 #3
dmbkiwi <dm*****@yahoo.com> writes:
On Tue, 26 Aug 2003 08:47:33 +0000, Sybren Stuvel wrote:

[...]
You seem to expect old HTML. Why not use XHTML only ('tidy' can
convert between them) and use a regular XML parser? Much, much, much
easier! And you won't have to be afraid of messing up your regular
expressions ;-)

Sybren


XML would be nice, but unfortunately I have no choice as to the markup
language used by the site. It's a website on the world wide web, not a
site overwhich I have any control. My regular expressions are at the
mercy of the developers of that site.


You misunderstand. HTMLTidy (or its descendant, tidylib) reads ugly,
non-conformant HTML and spits out clean, conformant XHTML (or HTML).

uTidylib is a ctypes wrapper of tidylib.

import tidy
from cStringIO import StringIO
tidydoc = tidy.parseString(html)
s = StringIO()
tidydoc.write(s)
tidied_html = s.getvalue()
mxTidy is a wrapper of a shared-library-ized HTMLTidy.

from mx.Tidy import tidy
tidied_html = tidy(html)[2]
John
Jul 18 '05 #4

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

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
2
by: news.microsoft.com | last post by:
I need help design a reg exp. I am parsing an html file to get the input values, here is one example <input VALUE="Staff Writer" size=60 type="text" name="author"> Can I grab the value "Staff...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
I'm writing an app which is going to rely extremely heavily on the usage of regular expressions. I'm reading the docs but having trouble wrapping my head around some of this since it's all fairly...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
9
by: Rene | last post by:
I'm trying to basically remove chunks of html from a page but I must not be doing my regular expression correctly. What i'm trying with no avail. $site = preg_replace("/<!DOCTYPE(.|\s)*<div...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.