Extracting text from a string

T

Tempo

Hello. I am having a little trouble extracting text from a string. The
string that I am dealing with is pasted below, and I want to extract
the prices that are contained in the string below. Thanks in advanced
for any and all help. Thank you.



<span class="sale">
$14.99
</span>, <span class="sale">
$27.99
</span>, <span class="sale">
$66.99
</span>, <span class="sale">
$129.99
</span>, <span class="sale">
$254.99
</span>
 
C

Claudio Grondi

Tempo said:
Hello. I am having a little trouble extracting text from a string. The
string that I am dealing with is pasted below, and I want to extract
the prices that are contained in the string below. Thanks in advanced
for any and all help. Thank you.



<span class="sale">
$14.99
</span>, <span class="sale">
$27.99
</span>, <span class="sale">
$66.99
</span>, <span class="sale">
$129.99
</span>, <span class="sale">
$254.99
</span>
What have you tried?

Taking a look into regular expressions in the Python tutorial (4.2 re --
Regular expression operations) should give you all you need.

Claudio Grondi
 
T

Tempo

Okay, so it sounds like I am in the right direction. However, I am not
sure that the text is in a string or some other format becasue the
string is enclosed in "[" and "]", not in ' '.
 
C

Claudio Grondi

Tempo said:
Okay, so it sounds like I am in the right direction. However, I am not
sure that the text is in a string or some other format becasue the
string is enclosed in "[" and "]", not in ' '.
In case you have it like ['the string'], the actual string will be:
['the string'][0].
Just use IDLE to test your ideas:
>>> ['the string'][0] 'the string'
>>>

Claudio Grondi
 
T

Tempo

This is the output I get:


[<span class="sale">
$14.99
</span>, <span class="sale">
$27.99
</span>, <span class="sale">
$66.99
</span>, <span class="sale">
$129.99
</span>, <span class="sale">
$254.99
 
B

bearophileHUGS

Tempo:
I am having a little trouble extracting text from a string. The
string that I am dealing with is pasted below, and I want to
extract the prices that are contained in the string below.

This may help:
['66.99', '.99']

You can read about Python regular expressions:
http://www.amk.ca/python/howto/regex/
http://docs.python.org/lib/module-re.html

------------------------

Perl 6 regular expressions are verbose by default, future Python may do
the same.
From Apocalypse 5, by Larry Wall: http://dev.perl.org/perl6/doc/design/apo/A05.html

In real life, tokens are more recognizable if they are separated by whitespace.<
Now, you may rightly point out that + is something we already have, and we already introduced /x to allow whitespace, so why is this bullet point here? Well, there's a lot of inertia in culture, and the problem with /x is that it's not the default, so people don't think to turn it on when it would probably do a lot of good. The culture is biased in the wrong direction. Whitespace around tokens should be the norm, not the exception. It should be acceptable to use whitespace to separate tokens that could be confused.<

Bye,
bearophile
 
S

samir

Saluton!

> I am having a little trouble extracting text from a string. The
> string that I am dealing with is pasted below, and I want to
> extract the prices that are contained in the string below.

This string is absolutely an XML chunk.
Just use xmllib.

Adiaux
Samir
 
C

Claudio Grondi

Tempo said:
This is the output I get:




[<span class="sale">
$14.99
</span>, <span class="sale">
$27.99
</span>, <span class="sale">
$66.99
</span>, <span class="sale">
$129.99
</span>, <span class="sale">
$254.99
</span>]
I think, guessing your knowledge level in Python, the best solution for
you to start with and learn some Python basics will be :
>>> prices.split()[2] '$14.99'
>>> prices.split()[6] '$27.99'
>>> prices.split()[10] '$66.99'
>>> prices.split()[14] '$129.99'
>>> prices.split()[18]
'$254.99'

Claudio Grondi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top