# 【已解决】Python中将以e为底的自然对数字符串转换为float浮点数

Python 9677浏览

【问题】

 2.1690427e-005 -1.1694737e-003 -6.1193734e-004 8.9455025e-004 -8.6277081e-004 -7.2735757e-004

【解决过程】

1.参考：

python里面自然对数的底怎么表示？

```import math;
print "math.e=",math.e;```

 math.e= 2.71828182846

2.知道有个math.exp了，

math.exp(x)

Return e**x.

3.但是看到了：

math.pow(x, y)

Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

Changed in version 2.6: The outcome of 1**nan and nan**0 was undefined.

```#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:

http://zhidao.baidu.com/question/506139033.html

Author:     Crifan Li
Version:    2012-12-13
Contact:    admin at crifan dot com
"""

import re;
import math;

def parseIntEValue():
print "math.e=",math.e; #math.e= 2.71828182846

intEValuesStr = """2.1690427e-005 -1.1694737e-003 -6.1193734e-004
8.9455025e-004 -8.6277081e-004 -7.2735757e-004""";
print "intEValuesStr=",intEValuesStr;
intEStrList = re.findall("-?\d+\.\d+e-\d+", intEValuesStr);
print "intEStrList=",intEStrList;
for eachIntEStr in intEStrList:
# intValue = int(eachIntEStr);
# print "intValue=",intValue;
foundEPower = re.search("(?P<intPart>-?\d+\.\d+)e(?P<ePower>-\d+)", eachIntEStr);
#print "foundEPower=",foundEPower;
if(foundEPower):
intPart = foundEPower.group("intPart");
ePower = foundEPower.group("ePower");
#print "intPart=%s,ePower=%s"%(intPart, ePower);
intPartValue = float(intPart);
ePowerValue = float(ePower);
print "intPartValue=%f,ePowerValue=%f"%(intPartValue, ePowerValue);
wholeOrigValue = intPartValue * math.pow(math.e, ePowerValue);
print "wholeOrigValue=",wholeOrigValue;

if __name__ == "__main__":
parseIntEValue();```

```#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:

http://zhidao.baidu.com/question/506139033.html

Author:     Crifan Li
Version:    2012-12-13
Contact:    admin at crifan dot com
"""

import re;
import math;

def ConvertELogStrToValue(eLogStr):
"""
convert string of natural logarithm base of E to value
return (convertOK, convertedValue)
eg:
input:  -1.1694737e-003
output: -0.0582246670563

input:  8.9455025e-004
output: 0.163843
"""

(convertOK, convertedValue) = (False, 0.0);
foundEPower = re.search("(?P<coefficientPart>-?\d+\.\d+)e(?P<ePowerPart>-\d+)", eLogStr, re.I);
#print "foundEPower=",foundEPower;
if(foundEPower):
coefficientPart = foundEPower.group("coefficientPart");
ePowerPart = foundEPower.group("ePowerPart");
#print "coefficientPart=%s,ePower=%s"%(coefficientPart, ePower);
coefficientValue = float(coefficientPart);
ePowerValue = float(ePowerPart);
#print "coefficientValue=%f,ePowerValue=%f"%(coefficientValue, ePowerValue);
#math.e= 2.71828182846
wholeOrigValue = coefficientValue * math.pow(math.e, ePowerValue);
#print "wholeOrigValue=",wholeOrigValue;

(convertOK, convertedValue) = (True, wholeOrigValue);
else:
(convertOK, convertedValue) = (False, 0.0);

return (convertOK, convertedValue);

def parseIntEValue():
intEValuesStr = """2.1690427e-005 -1.1694737e-003 -6.1193734e-004
8.9455025e-004 -8.6277081e-004 -7.2735757e-004""";
print "intEValuesStr=",intEValuesStr;
intEStrList = re.findall("-?\d+\.\d+e-\d+", intEValuesStr);
print "intEStrList=",intEStrList;
for eachIntEStr in intEStrList:
# intValue = int(eachIntEStr);
# print "intValue=",intValue;
(convertOK, convertedValue) = ConvertELogStrToValue(eachIntEStr);
#print "convertOK=%s,convertedValue=%f"%(convertOK, convertedValue);
print "eachIntEStr=%s,\tconvertedValue=%f"%(eachIntEStr, convertedValue);

# intEValuesStr= 2.1690427e-005 -1.1694737e-003 -6.1193734e-004
# 8.9455025e-004 -8.6277081e-004 -7.2735757e-004
# intEStrList= ['2.1690427e-005', '-1.1694737e-003', '-6.1193734e-004', '8.9455025e-004', '-8.6277081e-004', '-7.2735757e-004']
# eachIntEStr=2.1690427e-005,     convertedValue=0.014615
# eachIntEStr=-1.1694737e-003,    convertedValue=-0.058225
# eachIntEStr=-6.1193734e-004,    convertedValue=-0.112080
# eachIntEStr=8.9455025e-004,     convertedValue=0.163843
# eachIntEStr=-8.6277081e-004,    convertedValue=-0.158022
# eachIntEStr=-7.2735757e-004,    convertedValue=-0.133220

if __name__ == "__main__":
parseIntEValue();```

【总结】

Python中，没有直接可以把以e为底的自然对数的字符串，强制转换成对应的数值的。

### 网友最新评论 (2)

1. 简单来讲, 楼主是在一本正经地胡说八道. 这里的 e 是科学计数法, 1e-6 表示 1 * 10^(-6) 即 0.000001, 2.1690427e-005 表示的是 0.000021690427, 和自然对数完全不是一回事. Python 语言层面本来就支持科学计数法, 直接 1e-6 这样就是数值, 如果是字符串要转成数值的话 float('2.1690427e-005') 就行. 1e-6 和 0.000001 是一样的东西, 没有区别.
Lee.MaRS7年前 (2015-11-30)回复
• 楼主探索的精神可嘉，但是在误人子弟, 希望找到这篇文章的人能看到这个评论
pyyy6年前 (2016-12-05)回复
94 queries in 0.123 seconds, using 20.55MB memory