猜測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
如有問題,望糾正.當然有更好更簡單的方法歡迎提出