python浮点数转科学计数,Python:将字符串(科学计数形式)转换为浮点型

该博客讲述了如何使用numpy的genfromtxt函数从大型CSV文件中导入特定列,并将包含科学计数法的字符串转换为浮点数。作者展示了导入数据的代码,并提出遇到的问题是科学计数法的转换。解决方案是使用Python内置的float函数,它能够处理科学计数法的字符串。
摘要由CSDN通过智能技术生成

I'm trying to import a large .csv file containing text and numbers using genfromtxt in numpy. I'm only interested in two columns. I have most of the import sorted out with:

def importfile(root):

data = root.entry.get()

atw = np.genfromtxt(data, delimiter=",",

skip_header=1,

skip_footer=2,

autostrip=True,

usecols=(25,26),

dtype=("|S10"))

elem = atw[:,0]

concs = atw[:,1]

print(elem)

print(concs)

With output for elem and concs respectively:

['Na2O' 'MgO' 'Al2O3' 'SiO2' 'P2O5' 'SO3' 'Cl' 'K2O' 'CaO' 'TiO2' 'Cr2O3'

'MnO' 'FeO' 'NiO' 'Cu2O' 'ZnO' 'Ga2O3' 'SrO' 'Y2O3']

['3.76E+00' '1.31E+01' '1.14E+01' '4.04E+01' '1.24E+00' '5.89E-02'

'2.43E-02' '1.53E+00' '1.49E+01' '2.87E+00' '6.05E-02' '1.96E-01'

'1.17E+01' '3.69E-02' '8.73E-03' '1.39E-02' '1.93E-03' '1.88E-01'

'5.58E-03']

I have tried many different things for converting the concs string into a float, but it doesn't seem to like the fact that the concs are in scientific notation.... does there exist a way to turn the concs values into a float? Thanks in advance for your support.

解决方案

The float function can do this:

>>> float('1.31E+01')

13.1

or for a list:

>>> map(float, ['3.76E+00', '1.31E+01', '1.14E+01'])

[3.76, 13.1, 11.4]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值