Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String ValueError:无法将字符串转换为浮点:_String_Python 2.7_Floating Point - Fatal编程技术网

String ValueError:无法将字符串转换为浮点:

String ValueError:无法将字符串转换为浮点:,string,python-2.7,floating-point,String,Python 2.7,Floating Point,我对以下代码有问题: inputf = open('test.dat', 'r') lines = inputf.readlines() rico_clus_e = [] for line in lines: line.split() print line if (line[0] != '#'): rico_clus_e.append(float(line[4])) inputf.close() 我的test.dat文件是: # specn rico_a

我对以下代码有问题:

inputf = open('test.dat', 'r')
lines = inputf.readlines()
rico_clus_e = []
for line in lines:
    line.split()
    print line
    if (line[0] != '#'):
        rico_clus_e.append(float(line[4]))
inputf.close()
我的test.dat文件是:

# specn rico_all        rico_all_e  rico_clus   rico_clus_e rico_farclust   rico_far_e  extin
a119        1.07038692  0.11109547  0.61473431  0.15063627  0.32590239      0.14777812  0.207
这将在我的终端中提供以下输出:

# specn rico_all        rico_all_e  rico_clus   rico_clus_e rico_farclust   rico_far_e  extin

a119        1.07038692  0.11109547  0.61473431  0.15063627  0.32590239      0.14777812  0.207

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    rico_clus_e.append(float(line[4]))
ValueError: could not convert string to float: 
\specn rico\u all rico\u all e rico\u clus rico\u clus e rico\u farclust rico\u far\u e dese
a119 1.07038692 0.11109547 0.61473431 0.15063627 0.32590239 0.14777812 0.207
回溯(最近一次呼叫最后一次):
文件“test.py”,第8行,在
rico_clus_e.append(浮动(第[4]行))
ValueError:无法将字符串转换为浮点:

我对此感到很困惑。这与空间无关,我都检查过了。如果你用1,2或3来改变4,这是可行的,所以它一定与test.dat文件有关,但我似乎不知道怎么做。我正在使用python 2.7.3。

line.split()
本身对
line
没有任何作用。您必须存储方法调用的结果并使用它。

元素的表示形式(提示:
repr()
)是…?表示形式是“”,但为什么它适用于其他值?将4更改为3、2或1确实有效。。。对于数字3,表示为“9”,对于元素2,表示为“1”,这是因为
行[3]
'9'
,而
行[2]
'1'
。而
第[0]
行是
'a'
。好的,我现在明白你的回答了。它认为
'9'
是内部的东西,但它只是指字符串
line=line.split()。谢谢!