-3

I scrape an stock price from an website and the output ist e.g. "12 EUR". How can i remove the EUR from the string inpython , so that i can work with just the number?

Thanks a lot!

4 Answers4

1

Use regex to remove anything that is not a number.

import re
stock = '12 EUR'
stock = re.sub('[^0-9]', '', stock)
mismaah
  • 344
  • 5
  • 21
0

Something like:

amount = "12 EUR"
amount.split(' ')[0]

Output:

'12'
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
0

The following does not throw an exception when " EUR" is missing:

txt = '123 EUR'
txt = txt.strip(' EUR')
print(txt)
ChaimG
  • 7,024
  • 4
  • 38
  • 46
0

Building on mismaahs answer, a general purpose solution that handles currencies and decimals is:

import re
stock = '1,234.56 EUR'
stock = re.sub('^0-9\.', '', stock)
ChaimG
  • 7,024
  • 4
  • 38
  • 46