Extracting text from a string

Discussion in 'Python' started by Tempo, Sep 7, 2006.

  1. Tempo

    Tempo Guest

    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>
     
    Tempo, Sep 7, 2006
    #1
    1. Advertisements

  2. 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
     
    Claudio Grondi, Sep 7, 2006
    #2
    1. Advertisements

  3. Tempo

    Tempo Guest

    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 ' '.
     
    Tempo, Sep 7, 2006
    #3
  4. In case you have it like ['the string'], the actual string will be:
    ['the string'][0].
    Just use IDLE to test your ideas:
    Claudio Grondi
     
    Claudio Grondi, Sep 7, 2006
    #4
  5. Tempo

    Tempo Guest

    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
     
    Tempo, Sep 7, 2006
    #5
  6. Tempo:
    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.
    Bye,
    bearophile
     
    bearophileHUGS, Sep 7, 2006
    #6
  7. Tempo

    samir Guest

    Saluton!

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

    Adiaux
    Samir
     
    samir, Sep 7, 2006
    #7
  8. I think, guessing your knowledge level in Python, the best solution for
    you to start with and learn some Python basics will be :
    '$254.99'

    Claudio Grondi
     
    Claudio Grondi, Sep 7, 2006
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.