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

 

如有問題,望糾正.當然有更好更簡單的方法歡迎提出


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM