Extracting text from a string

Tim Williams tim at tdw.net
Thu Sep 7 19:58:04 EDT 2006


On 07/09/06, Anthra Norell <anthra.norell at tiscalinet.ch> wrote:
> s = '''<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>'''
>
> >>> for line in [l.strip () for l in s.splitlines ()]:
>           if line [0] == '$': print line
>
> $14.99
> $27.99
> $66.99
> $129.99
> $254.99
>
> Why parse? Why regular expressions?
>


>>> print '\n'.join([i for i in s.splitlines() if i[0] == '$'])
$14.99
$27.99
$66.99
$129.99
$254.99

and
>>> print '\n'.join([i.strip() for i in s.splitlines () if i [0] == '$'])
parses with no trailing whitespace

:)



More information about the Python-list mailing list