我运行了这段代码,遇到了下面的错误:我如何才能避开错误并继续下一个wrod?
GLOVE_DATASET_PATH = 'glove.840B.300d.txt'
from tqdm import tqdm
import string
embeddings_index = {}
f = open(GLOVE_DATASET_PATH, encoding="utf8")
word_counter = 0
for line in tqdm(f):
values = line.split()
word = values[0]
if word in dictionary:
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
word_counter += 1
f.close()
print('Found %s word vectors matching enron data set.' % len(embeddings_index))
print('Total words in GloVe data set: %s' % word_counter)错误消息是ValueError:无法将字符串转换为浮点型:‘’on line -> 12 coef= np.asarray(values1:,dtype='float32')
发布于 2021-09-22 09:29:18
您可以抑制它:
from contextlib import suppress
with suppress(ValueError):
coefs = np.asarray(values[1:], dtype='float32')发布于 2021-09-22 10:01:01
当有事情发生时,我会试着找出发生了什么:
if word in dictionary:
try:
coefs = np.asarray(values[1:], dtype='float32')
except:
print(values[1:], type(values[1:]))
coefs = 0
embeddings_index[word] = coefs这将打印出您获得的返回值,但无法转换,并在此时在列表中写入一个0。
https://stackoverflow.com/questions/69281452
复制相似问题