-1

How can I extract the prices from a list that contains for example:

[<span class="primary-price">$61,989</span>,
 <span class="primary-price">$21,905</span>,
 <span class="primary-price">$20,595</span>]

It should give me back: prices = [61.989,21.905,20.595] or at least prices = [$61,989, $21,905, $20,595]

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
deagle
  • 1

1 Answers1

1

You can do this with regular expression (pattern matching)

import re

string = '''[<span class="primary-price">$61,989</span>,
 <span class="primary-price">$21,905</span>,
 <span class="primary-price">$20,595</span>]'''

numbers = re.findall('\$(\d+,\d+)', string)

You can also replace the comma(,) and convert it to integers

numbers = [int(num.replace(',', '')) for num in numbers ]
print(numbers)