473,810 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String parsing


The string below is a piece of a longer string of about 20000
characters returned from a web page. I need to isolate the number at
the end of the line containing 'LastUpdated'. I can find
'LastUpdated' with .find but not sure about how to isolate the
number. 'LastUpdated' is guaranteed to occur only once. Would
appreciate it if one of you string parsing whizzes would take a stab
at it.

Thanks,

jh

<input type="hidden" name="RFP" value="-1"/>
<!--<input type="hidden" name="EnteredBy " value="johnxxxx "/>-->
<input type="hidden" name="EnteredBy " value="john"/>
<input type="hidden" name="ServiceIn dex" value="1"/>
<input type="hidden" name="LastUpdat ed" value="11786588 63"/>
<input type="hidden" name="NextPage" value="../active/active.php"/>
<input type="hidden" name="ExistingS tatus" value="10" ?>
<table width="98%" cellpadding="0" cellspacing="0" border="0"
align="center"

May 9 '07 #1
12 3102
En Tue, 08 May 2007 22:09:52 -0300, HMS Surprise <jo**@datavoice int.com>
escribió:
The string below is a piece of a longer string of about 20000
characters returned from a web page. I need to isolate the number at
the end of the line containing 'LastUpdated'. I can find
'LastUpdated' with .find but not sure about how to isolate the
number. 'LastUpdated' is guaranteed to occur only once. Would
appreciate it if one of you string parsing whizzes would take a stab
at it.
<input type="hidden" name="RFP" value="-1"/>
<!--<input type="hidden" name="EnteredBy " value="johnxxxx "/>-->
<input type="hidden" name="EnteredBy " value="john"/>
<input type="hidden" name="ServiceIn dex" value="1"/>
<input type="hidden" name="LastUpdat ed" value="11786588 63"/>
<input type="hidden" name="NextPage" value="../active/active.php"/>
<input type="hidden" name="ExistingS tatus" value="10" ?>
<table width="98%" cellpadding="0" cellspacing="0" border="0"
align="center"
You really should use an html parser here. But assuming that the page will
not change a lot its structure you could use a regular expression like
this:

expr = re.compile(r'na me\s*=\s*"LastU pdated"\s+value \s*=\s*"(.*?)"' ,
re.IGNORECASE)
number = expr.search(tex t).group(1)
(Handling of "not found" and "duplicate" cases is left as an exercise for
the reader)

Note that <input value="11786588 63" type="hidden" name="LastUpdat ed" /is
as valid as your html, but won't match the expression.

--
Gabriel Genellina

May 9 '07 #2
On 8 May 2007 18:09:52 -0700, HMS Surprise <jo**@datavoice int.comwrote:
>
The string below is a piece of a longer string of about 20000
characters returned from a web page. I need to isolate the number at
the end of the line containing 'LastUpdated'. I can find
'LastUpdated' with .find but not sure about how to isolate the
number. 'LastUpdated' is guaranteed to occur only once. Would
appreciate it if one of you string parsing whizzes would take a stab
at it.
Does this help?

In [7]: s = '<input type="hidden" name="LastUpdat ed"
value="11786588 63"/>'

In [8]: int(s.split("=" )[-1].split('"')[1])
Out[8]: 1178658863

There's probably a hundred different ways of doing this, but this is
the first that came to mind.

Cheers,

Tim
Thanks,

jh

<input type="hidden" name="RFP" value="-1"/>
<!--<input type="hidden" name="EnteredBy " value="johnxxxx "/>-->
<input type="hidden" name="EnteredBy " value="john"/>
<input type="hidden" name="ServiceIn dex" value="1"/>
<input type="hidden" name="LastUpdat ed" value="11786588 63"/>
<input type="hidden" name="NextPage" value="../active/active.php"/>
<input type="hidden" name="ExistingS tatus" value="10" ?>
<table width="98%" cellpadding="0" cellspacing="0" border="0"
align="center"

--
http://mail.python.org/mailman/listinfo/python-list
May 9 '07 #3
Thanks for posting. Could you reccommend an HTML parser that can be
used with python or jython?
john
May 9 '07 #4
Yes it could, after I isolate that one string. Making sure I that I
isolate that complete line and only that line is part of the problem.

thanks for posting.

jh

May 9 '07 #5
On May 8, 9:19 pm, HMS Surprise <j...@datavoice int.comwrote:
Yes it could, after I isolate that one string. Making sure I that I
isolate that complete line and only that line is part of the problem.
It comes in as one large string...
May 9 '07 #6
En Tue, 08 May 2007 23:06:14 -0300, HMS Surprise <jo**@datavoice int.com>
escribió:
Thanks for posting. Could you reccommend an HTML parser that can be
used with python or jython?
Try BeautifoulSoup, which handles malformed pages pretty well.

--
Gabriel Genellina

May 9 '07 #7
On 8 May 2007 19:06:14 -0700, HMS Surprise wrote
Thanks for posting. Could you reccommend an HTML parser that can be
used with python or jython?
BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/) makes HTML
parsing easy as pie, and sufficiently old versions seem to work with Jython. I
just tested this with Jython 2.2a1 and BeautifulSoup 1.x:

Jython 2.2a1 on java1.5.0_07 (JIT: null)
Type "copyright" , "credits" or "license" for more information.
>>from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(" ""<input type="hidden" name="LastUpdat ed"
value="11786588 63"/>""")
>>print soup.first('inp ut', {'name':'LastUp dated'}).get('v alue')
1178658863

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net

May 9 '07 #8
Thanks all.

Carsten, you are here early and late. Do you ever sleep? ;^)

May 9 '07 #9
This looks to be simple HTML (and I'm presuming that's a type on
that ?ending). A quick glance at the Python library reference (you do
have a copy, don't you) reveals at least two HTML parsing modules...
No that is not a typo and bears investigation. Thanks for the find.

I found HTMLParser but had trouble setting it up.
About five minutes work gave me this:
My effort has been orders of magnitude greater in time.....

Thanks all for all the excellent suggestions.
jh

May 9 '07 #10

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

Similar topics

26
808
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a .txt-file in which the data is structured in that way: Project-Nr. ID name lastname 33 9 Lars Lundel 33 12 Emil Korla
18
7199
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1 QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2 AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2 AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
50
4965
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse method to accept formating that i need. if i do console.writeLine DateTime.Now.ToString it gives the format: YYYY-MM-DD HH:MM:SS TIA, Z.
9
14126
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6). so i wonder is there any faster way to parse string(char array) into float number.
4
6425
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but it seems C# does not support the timezone letters (CST). I suppose to use the symbol ZZZ for the timezone letters, but MSDE pointed out that it is not supported in C#, isn't it?
3
2086
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
9
4557
balabaster
by: balabaster | last post by:
I'm looking for some ideas regarding string parsing and brackets. Say I have the following string: 56*(73+23/(28+(7/14)-(3/2)) What would be the best way to parse the string for each opening bracket's closing bracket? Is there a standard way of doing this out there somewhere that I've failed to find? As you can see by parsing the string visually, we're missing a closing bracket, and a simple count of opening braces and closing...
6
3520
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given the string: "AAANN" it would return a string containing 3 alphanumeric characters followed by 3 digits. This part I have managed:) I would now like to add some complexity to this, such as repetitions and grouping. For example, I'd like to have...
6
4738
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following format: struct mst_ {
1
4411
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or Stack code and parsing code my professor i does not like me using buffer reader on my code and my professor did even give me an example code for parsing as well as pop push top or Stack code and i don't know how to do this code into parsing and pop push...
0
9722
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
9603
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,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10393
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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
7664
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
6882
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
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.