【问题】
有这样的字符串,是以e为底的,自然对数值:
2.1690427e-005 -1.1694737e-003 -6.1193734e-004 8.9455025e-004 -8.6277081e-004 -7.2735757e-004 |
想要将每个值,都转换为对应的浮点数值。
【解决过程】
1.参考:
和
去先试试:
1 2 | 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的值,和对应的前缀的值,就可以获得对应数据了。
所以去尝试写代码,好像就可以了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 关于python的sys 模块问题 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(); |
然后再封装成函数,就变成了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 关于python的sys 模块问题 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为底的自然对数的字符串,强制转换成对应的数值的。
需要自己实现对应的代码才行。