-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!

  • There's a whole bunch of ways to do this. Do you have to handle only `EUR`? Is there guaranteed to be a space between the number and the currency? Is the currency guaranteed to be spelled out (and not a symbol like US$)? Please take the [tour], read [what's on-topic here](/help/on-topic) and [ask], and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a specific question about your algorithm or technique. What did you try, and in what way did it not work? – Pranav Hosangadi Sep 23 '20 at 15:55

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
  • 307
  • 2
  • 18
0

Something like:

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

Output:

'12'
Jack Fleeting
  • 20,849
  • 6
  • 20
  • 43
0

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

txt = '123 EUR'
txt = txt.strip(' EUR')
print(txt)
ChaimG
  • 5,936
  • 4
  • 30
  • 45
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
  • 5,936
  • 4
  • 30
  • 45