I have a rather distorted code that almost produces a tangible price / book from Yahoo Finance for companies (a good module called ystockquotegets an intangible price / book value already).
My problem is this:
For one of the variables in the calculation it matters , I get strings like 10.89B and 4.9M , where B and M are for billions and millions, respectively . I have problems converting them to numbers, here where I am:
shares=''.join(node.findAll(text=True)).strip().replace('M','000000').replace('B','000000000').replace('.','') for node in soup2.findAll('td')[110:112]
It's pretty dirty, but I think it will work if instead
.replace('M','000000').replace('B','000000000').replace('.','')
I used a regex with variables. I guess the question is simple regex and variables. Other suggestions are also good.
EDIT:
To be specific, I hope to get something that works for numbers with zero, one, or two decimal places, but these answers all look useful.
source
share