我正在尝试读取包含字符串数字的文件,但是当我尝试将字符串数字转换为float时,它会给我错误。我知道这可能是因为数字大,但有没有其他方法将字符串数转换为浮点数?我已经查看了现有的解决方案,但我没有找到相关的解决方案。
def load_embeddings(self, FileName = \
'Resources/EN-wform.w.5.cbow.neg10.400.subsmpl.txt'):
file = open(FileName,'r')
for line in file:
tokens = line.split('\t')
#since each line's last token content '\n'
# we need to remove that
tokens[-1] = str(tokens[-1].split())
#each line has 400 tokens
for i in xrange(1, len(tokens)):
# pass
print "token ", tokens[i]
print type(tokens[i])
print float(tokens[i])
输出错误:
token 0.017045999999999999
<type 'str'>
0.017046
token 0.038705999999999997
<type 'str'>
0.038706
token 0.033797000000000001
<type 'str'>
0.033797
token ['0.016487999999999999']
<type 'str'>
Traceback (most recent call last):
File "featureExtraction.py", line 51, in <module>
f.load_embeddings()
File "featureExtraction.py", line 38, in load_embeddings
print float(tokens[i])
ValueError: could not convert string to float: ['0.016487999999999999']