最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Python crifan 10573浏览 0评论

【问题】

有这样的字符串,是以e为底的,自然对数值:

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

想要将每个值,都转换为对应的浮点数值。

【解决过程】

1.参考:

在python中怎么利用自然对数e?

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

去先试试:

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

可以打印出对应的值:

math.e= 2.71828182846

2.知道有个math.exp了,

就去Python手册中找找对应语法。

找到的是:

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.

所以,想要了,先去解析上述数据,得到对应的e的power的值,和对应的前缀的值,就可以获得对应数据了。

所以去尝试写代码,好像就可以了:

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
关于python的sys 模块问题
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:
关于python的sys 模块问题
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为底的自然对数的字符串,强制转换成对应的数值的。

需要自己实现对应的代码才行。

转载请注明:在路上 » 【已解决】Python中将以e为底的自然对数字符串转换为float浮点数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (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.MaRS9年前 (2015-11-30)回复
    • 楼主探索的精神可嘉,但是在误人子弟, 希望找到这篇文章的人能看到这个评论
      pyyy8年前 (2016-12-05)回复
88 queries in 0.187 seconds, using 22.01MB memory