python - Convert string with engineer prefix to float -


I have a string like '102.3k' I replace this string with an engineer Prefix marking in a float number

Allowed prefixes

  posPrefixes = ['k', 'm', 'j', 't', 'p' Negroprefixes = '' '' '' '' '' '' '' '' '' '' '' '' '' '' / P>  p> μ means 10 ^ -6  

I think I should use regex to do this, but I have a lot of experience with regex.

Edit: Ideally the solution should also be enabled to convert any wire '102.3' (without the prefix) should also be converted to float

Try this, no regex required:

  pos_postfixes = ['k', 'm', 'j ['M', 'μ', 'n', 'p', 'f', 'a', '', 'p', 'p', 'e', ​​'z', 'y'] neg_postfixes = Num_postfix = n [-1] if pos_postfixes in num_postfix: num = float n [: - 1]) number * = 10 ** ((pos_p Num_postfixes: num = float (n [: - 1] ) Num * = 10 ** (- (neg_postfixes.index (num_postfix) +1) * 3) Other Th ostfixes.index (num_postfix) +1) * 3) elif num_postfix: Num = float (n) print (num)  

The point of note is that in Python, the comparison of therm The names of at least variable names are more common to use, PEP-8:


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -