python - 有没有办法逃避错误并转到下一个单词?
问题描述
我运行此代码并遇到以下错误:如何逃避错误并继续下一个 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')
解决方案
你可以压制它:
from contextlib import suppress
with suppress(ValueError):
coefs = np.asarray(values[1:], dtype='float32')
推荐阅读
- typescript - npm 包的类在 typescript 和 transpiled 版本之间不兼容
- c# - 最大行列表视图 xamarin
- c# - AccessViolationException 将数组传递给本机函数
- java - 我应该使用什么端口号
- javascript - JS 中的这种语言结构是什么?
- html - 有没有办法清除/隐藏两个 td 表中的第一个 td,而无需访问第一个 td?
- selenium - 使用 * vs 元素标签
- sql - 大于表达式中的 SQL 多列
- mysql - log4jdbc可以和spring boot一起使用吗?
- groovy - 在groovy中将十六进制字符串转换为二进制