首页 > 解决方案 > 有没有办法逃避错误并转到下一个单词?

问题描述

我运行此代码并遇到以下错误:如何逃避错误并继续下一个 wod?

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: could not convert string to float: '.' 在线---> 12 coefs = np.asarray(values[1:], dtype='float32')

标签: python

解决方案


你可以压制它:

from contextlib import suppress
with suppress(ValueError):
    coefs = np.asarray(values[1:], dtype='float32')

推荐阅读