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

【问题】

有这样的字符串,是以e为底的,自然对数值:2.1690427e-005 -1.1694737e-003 -6.1193734e-004

8.9455025e-004 -8.6277081e-004 -7.2735757e-004

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

【解决过程】

1.参考:

去先试试: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-?\d+\.\d+)e(?P-\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-?\d+\.\d+)e(?P-\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为底的自然对数的字符串,强制转换成对应的数值的。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值