Hi,
I've experimented with regular expressions to solve my problems in the
past but I have seen so many comments about HTMLParser and sgmllib that
I thought I would try a different approach this time so I tried using
HTMLParser.
I want to search through my SGML file for various strings of text and
find out what section they're in. What I have here does this to a
certain extent but I was wondering if I could make handle_data and
regular expressions work together to make this work a little better.
For instance, when I search for "above" as I am here, I just get
something like this: '174.114[1]':'above' but this isn't very useful
b/c I want to know the context of above (i.e., the informaiton on
either side the above) and maybe even us a regular expression to filter
the search a little more.
Any ideas?
As always, I'd appreciate feedback on my efforts.
Thanks,
Greg
###
from HTMLParser import HTMLParser
import os, re
root = raw_input("Ente r the path where the program should run: ")
fname = raw_input("Ente r name of the file: ")
print
given,ext = os.path.splitex t(fname)
inputFile = open(os.path.jo in(root,fname), 'r')
data = inputFile.read( )
class PartFinder(HTML Parser):
_full = None
_secDict = dict()
def found(self):
return self._secDict
def handle_starttag (self, tag, attrs):
if tag == "sec-main":
self._main = dict(attrs).get ('no')
self._full = self._main
if tag == "sec-sub1":
self._subone = dict(attrs).get ('no')
self._full = self._main + '[' + self._subone + ']'
if tag == "sec-sub2":
self._subtwo = dict(attrs).get ('no')
self._full = self._main + '[' + self._subone + ']' + '['
+ self._subtwo + ']'
def handle_data(sel f, data):
if "Pt" in data:
if not self._secDict.h as_key(self._ma in):
self._secDict[self._full] = [data]
print self._secDict
if __name__ == "__main__":
parser = PartFinder()
parser.feed(dat a)
x = parser.found()
output_part = given + '.parts'
outputFile = file(os.path.jo in(root,output_ part), 'w')
outputFile.writ e(str(x))
outputFile.clos e() 0 1009 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris Lasher |
last post by:
Hello,
I would like to create a set of very similar regular expression. In
my initial thought, I'd hoped to create a regular expression with a
variable inside of it that I could simply pass a string into by
defining this variable elsewhere in my module/function/class where I
compile the regular expression, thus making for an easy substitution.
However, still after JFGI'ing, I still cannot find the syntax to place
a variable inside a...
|
by: norton |
last post by:
Hello,
Does any one know how to extact the following text into 4 different
groups(namely Date, Artist, Album and Quality)?
- Artist - Album
Artist - Album
- Artist - Album
- Artist - Album-
i have try this syntax but it failed
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I want to avoid
that. My question here is if there is a way to pass either a memory
stream or array of "find", "replace" expressions or any other way to
avoid multiple copies of a string.
Any help will be highly appreciated
|
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 the first regular expression
that matches the string.
I've gor the regular expressions ordered so that the highest priority is
first (if two or more regular expressions match the string I want the first
one returned)
The code that does this has...
|
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...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |