I have some rather mangled code that almost produces the tangible price/book from Yahoo Finance for companies (a nice module called ystockquote
gets the intangible price/book value already).
My problem is this:
For one of the variables in the calculation, shares outstanding I'm getting strings like 10.89B and 4.9M, where B and M stand respectively for billion and million. I'm having trouble converting them to numbers, here's where I'm at:
shares=''.join(node.findAll(text=True)).strip().replace('M','000000').replace('B','000000000').replace('.','') for node in soup2.findAll('td')[110:112]
Which is pretty messy, but I think it would work if instead of
.replace('M','000000').replace('B','000000000').replace('.','')
I was using a regular expression with variables. I guess the question is simply which regular expression and variables. Other suggestions are also good.
EDIT:
To be specific I'm hoping to have something that works for numbers with zero, one, or two decimals but these answers all look helpful.
join.strip.replace.replace.replace
constructs, they are hard to read and harder to debug. See python.org/dev/peps/pep-0008 and python.org/dev/peps/pep-0020 and keep to them wholly. Your future self will thank you. – Peculiumimport this
– Cristinecristionaimport antigravity
– Peculium