vandelay vandelay - 1 year ago 78
Python Question

An easy way to turn a string with non integer characters, to an integer

Is there any easy way to turn string like


Into an integer?

Or is it the rigid approach(Only one I can think of) to find index of '.' and 'M' and do some substringing

Answer Source

I am pretty sure that there is no direct way(i.e. a single function call) of getting the answer. But you could use a simple code like below.

valD = {"M" : 6, "B" : 9}

def getNumber(nstring):
    if nstring[-1] in valD:
        return int(float(nstring[:-1]) * 10**valD[nstring[-1]])
        return int(nstring)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download