python 科学计数法转数值

猜测python应该是有现成的模块可以解决该问题,不过没找到,所以自己简单写了个函数处理:

def tranform(inputString):

  num_value = re.compile('^[0-9.]+([*|x][0-9]+)+[E|e|^][+-]?[0-9]+$')

  result = num_value.match(inputString)
  if result:
    temp1 = float(1)
    res = re.match('^[0-9.]+([*|x][0-9]+)+', inputString)
    if (not res is None) and len(str(result)) > 0:
      if len(re.split('[*|x]', res.group(0))) > 1:
        for a in re.split('[*|x]', res.group(0)):
          temp1 = temp1 * float(a)
    temp2 = re.findall('[E|e|^][+-]?[0-9]+$', inputString)[0]
    inputTemp = str(temp1) + str(temp2)
    val = float(inputTemp.replace('^', 'E'))/10
    num_value = re.compile('^[-+]?[0-9]+\.?[0-9]?$')
    result = num_value.match(str(val))
    if result:
      return val

 

如有问题,望纠正.当然有更好更简单的方法欢迎提出

posted @ 2018-10-18 14:29  乐-张蒙  阅读(9659)  评论(1编辑  收藏  举报