0

I am trying to extract scientific notation from strings like:

`#DataCGSConversionFactor[0] = 1.0051900924519e-29`

Unfortunately because of the first 0 that isn't a part of the number I want, the other solutions I have tried don't work, giving:

ValueError: invalid literal for float(): 0]

Thanks in advance.

Josh H
  • 23
  • 2
  • Off the top of my head: split your string at =, and apply the parser to the second half. If you have more complicated formats, *post them* -- we aren't psychic, after all. – LSerni Nov 10 '14 at 22:54
  • Sorry, hadn't seen -- welcome to Stack Overflow. To maximize your chances of getting useable answers, it's recommended you try to post a SSCCE - see http://sscce.org/ – LSerni Nov 10 '14 at 22:57

1 Answers1

1

For the string you show,

x = "#DataCGSConversionFactor[0] = 1.0051900924519e-29"

f = float(x.split()[-1])    # split at spaces, take last item, and cast to a float

print f, type(f)
# 1.0051900924519e-29, float
tom10
  • 67,082
  • 10
  • 127
  • 137