Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54030 > unrolled thread
| Started by | Cory Mottice <cmottice@gmail.com> |
|---|---|
| First post | 2013-09-11 20:03 -0700 |
| Last post | 2013-09-12 10:36 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Parsing an html line and pulling out only numbers that meet a certain criteria Cory Mottice <cmottice@gmail.com> - 2013-09-11 20:03 -0700
Re: Parsing an html line and pulling out only numbers that meet a certain criteria John Gordon <gordon@panix.com> - 2013-09-12 05:40 +0000
Re: Parsing an html line and pulling out only numbers that meet a certain criteria Dave Angel <davea@davea.name> - 2013-09-12 10:36 +0000
| From | Cory Mottice <cmottice@gmail.com> |
|---|---|
| Date | 2013-09-11 20:03 -0700 |
| Subject | Parsing an html line and pulling out only numbers that meet a certain criteria |
| Message-ID | <d9a020cc-89eb-4cb4-b884-abe156f18d33@googlegroups.com> |
I am using line.rfind to parse a particular line of html code. For example, this is the line of html code I am parsing:
<strong class="temp">79<span>°</span></strong><span class="low"><span>Lo</span> 56<span>°</span></span>
and this is the code I use to split the line to (in this case) pull out the '79'.
position0 = line.rfind('{}'.format(date1.strftime("%a")))
if position0 > 0 :
self.high0 = lines[line_number + 4].split('<span>')[0].split('">')[-1]
Now I need to only pull out that number if it is >=94 and <=37. If it does not meet this criteria, I don't want anything to happen. Any ideas? Thank you in advance!
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-12 05:40 +0000 |
| Message-ID | <l0rk3s$qft$1@reader1.panix.com> |
| In reply to | #54030 |
In <d9a020cc-89eb-4cb4-b884-abe156f18d33@googlegroups.com> Cory Mottice <cmottice@gmail.com> writes:
> I am using line.rfind to parse a particular line of html code. For example, this is the line of html code I am parsing:
> <strong class="temp">79<span>°</span></strong><span class="low"><span>Lo</span> 56<span>°</span></span>
> and this is the code I use to split the line to (in this case) pull out the '79'.
> position0 = line.rfind('{}'.format(date1.strftime("%a")))
> if position0 > 0 :
> self.high0 = lines[line_number + 4].split('<span>')[0].split('">')[-1]
> Now I need to only pull out that number if it is >=94 and <=37. If it
> does not meet this criteria, I don't want anything to happen. Any ideas?
> Thank you in advance!
If you have the string value in a variable, you can call int() to convert
it to an integer, like so:
x = "57" # string value
y = int(x) # integer value
But what do you mean by you "don't want anything to happen"? Obviously
you have to do the work of grabbing the correct part of the line and
parsing it to an integer value; you can't magically undo that work if the
value isn't in the desired range.
Do you mean that you don't want to assign it to self.high0? That's easy;
assign it to a temporary variable first, and only assign it to self.high0
if it falls in the desired range.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-12 10:36 +0000 |
| Message-ID | <mailman.309.1378982206.5461.python-list@python.org> |
| In reply to | #54030 |
On 11/9/2013 23:03, Cory Mottice wrote:
> I am using line.rfind to parse a particular line of html code. For example, this is the line of html code I am parsing:
>
> <strong class="temp">79<span>°</span></strong><span class="low"><span>Lo</span> 56<span>°</span></span>
>
> and this is the code I use to split the line to (in this case) pull out the '79'.
>
> position0 = line.rfind('{}'.format(date1.strftime("%a")))
> if position0 > 0 :
> self.high0 = lines[line_number + 4].split('<span>')[0].split('">')[-1]
>
> Now I need to only pull out that number if it is >=94 and <=37. If it does not meet this criteria, I don't want anything to happen. Any ideas? Thank you in advance!
Since no integer is both >=94 and <=37, you'd never have to worry about
it. But assuming there's a typo there,
temp = lines[line_number + 4.........
if 94 <= int(temp) <= 37:
self.high0 = temp
--
DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web