Python - Parse a string with custom price units -


What is the fastest way to par int in Python? String can include custom units, such as "k" (kg), "m" (lakh), or "b" (billion).

For example:

  100 - & gt; 100 100k - & gt; 100000 100m - & gt; 100000000 100b - & gt; 100000000000   

thanks

  def converter (val) Try: # == 'K: 1000,' m ': 1000000,' b ': 1000000000} unit = val [-1] Try: number = int (val [: - 1]) except valueError: #search Unit In: Return Lookup [Unit] * Number Return Ent (Val)> gt; & Gt; & Gt; Print Convert ("100 M") & gt; & Gt; & Gt; 100000000   

Create a lookup table and then divide the numbers into numbers and units. If the unit is present, then see our table and use it to make the final number. Otherwise, return the number.

Comments