我運行以下Python腳本:ValueError異常:無法將字符串轉換爲float:ID
#!/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...]
我工作得很好。
任何人都可以解釋一下這個嗎? THX
這有助於捕獲csv文件中的空字符串。 –