python字符串转float出错_python-ValueError:无法将字符串转换为float:id

python-ValueError:无法将字符串转换为float:id

我正在运行以下python脚本:

#!/usr/bin/python

import os,sys

from scipy import stats

import numpy as np

f=open('data2.txt', 'r').readlines()

N=len(f)-1

for i in range(0,N):

w=f[i].split()

l1=w[1:8]

l2=w[8:15]

list1=[float(x) for x in l1]

list2=[float(x) for x in l2]

result=stats.ttest_ind(list1,list2)

print result[1]

但是我得到了类似的错误:

ValueError: could not convert string to float: id

我对此感到困惑。当我在交互式部分中仅尝试一行时,而不是使用脚本进行循环时:

>>> from scipy import stats

>>> import numpy as np

>>> f=open('data2.txt','r').readlines()

>>> w=f[1].split()

>>> l1=w[1:8]

>>> l2=w[8:15]

>>> list1=[float(x) for x in l1]

>>> list1

[5.3209183842, 4.6422726719, 4.3788135547, 5.9299061614, 5.9331108706, 5.0287087832, 4.57...]

它运作良好。

有人可以解释一下吗?谢谢。

7个解决方案

42 votes

显然,您的某些行没有有效的float数据,特别是某些行的文本id无法转换为float。

当您在交互式提示中尝试时,您仅尝试第一行,因此最好的方法是在出现此错误的地方打印行,您将知道错误的行,例如

#!/usr/bin/python

import os,sys

from scipy import stats

import numpy as np

f=open('data2.txt', 'r').readlines()

N=len(f)-1

for i in range(0,N):

w=f[i].split()

l1=w[1:8]

l2=w[8:15]

try:

list1=[float(x) for x in l1]

list2=[float(x) for x in l2]

except ValueError,e:

print "error",e,"on line",i

result=stats.ttest_ind(list1,list2)

print result[1]

Anurag Uniyal answered 2019-10-25T00:23:46Z

18 votes

我的错误非常简单:包含数据的文本文件的最后一行有一些空格(因此不可见)。

作为grep的输出,我有45,而不是45。

Sopalajo de Arrierez answered 2019-10-25T00:24:16Z

11 votes

该错误非常冗长:

ValueError: could not convert string to float: id

在文本文件中的某处,一行中包含单词2584116930927920122012,实际上不能将其转换为数字。

您的测试代码有效,因为line 2中没有单词id。

如果您想抓住那条线,请尝试以下代码。 我整理了一下代码:

#!/usr/bin/python

import os, sys

from scipy import stats

import numpy as np

for index, line in enumerate(open('data2.txt', 'r').readlines()):

w = line.split(' ')

l1 = w[1:8]

l2 = w[8:15]

try:

list1 = map(float, l1)

list2 = map(float, l2)

except ValueError:

print 'Line {i} is corrupt!'.format(i = index)'

break

result = stats.ttest_ind(list1, list2)

print result[1]

Blender answered 2019-10-25T00:25:00Z

4 votes

您的数据可能不是您所期望的-似乎您正在期望但没有得到浮动。

解决此问题的一种简单解决方案是在try循环中添加try / except:

for i in range(0,N):

w=f[i].split()

l1=w[1:8]

l2=w[8:15]

try:

list1=[float(x) for x in l1]

list2=[float(x) for x in l2]

except ValueError, e:

# report the error in some way that is helpful -- maybe print out i

result=stats.ttest_ind(list1,list2)

print result[1]

Matt Fenwick answered 2019-10-25T00:25:31Z

4 votes

也许您的数字实际上不是数字,而是伪装成数字的字母?

就我而言,我使用的字体表示“ l”和“ 1”看起来非常相似。 我有一个像“ l1919”的字符串,我以为是“ 11919”,这使事情搞砸了。

Tom Roth answered 2019-10-25T00:26:02Z

0 votes

检查原始csv文件中的数字,以查看数字上是否有双引号。

Haohan Li answered 2019-10-25T00:26:27Z

0 votes

我用熊猫的基本技术解决了类似的情况。 首先使用pandas加载csv或文本文件,这很简单

data=pd.read_excel('link to the file')

然后将数据索引设置为需要更改的相关列。 例如,如果您的数据将ID作为一个属性或一列,则将索引设置为ID。

data = data.set_index("ID")

然后使用以下命令删除所有以“ id”作为值而不是数字的行。

data = data.drop("id", axis=0).

希望这个能对您有所帮助。

Kapilfreeman answered 2019-10-25T00:27:11Z

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值