首页 > 解决方案 > 我该如何解决这个问题“NameError:名称'f'未定义”这是我的功能

问题描述

我的代码存在问题,我不知道如何定义 f 以便我的代码可以正常工作。请帮忙。

stopwords = []
try:
    f = open("https://github.com/Yoast/YoastSEO.js/blob/develop/src/config/stopwords.js", 'r')
    stopwords = f.read().split('\n')
except IOError:
    print('Problem opening file')
finally:
    f.close()
print('Sentence before stopwrods removed: \n', tokenized[51])
filtered = []
for t in tokenized:
    text = t[0]
    f_text = []
    for word in text:
        if word not in stopwords and len(word) > 2:
            f_text.append(word)
    filtered.append((f_text, t[1]))

print('\nSentence after stopwords removed: \n', filtered[51]

标签: pythonpandasjupyter-notebook

解决方案


stopwords = []

try:
    f = open("https://github.com/Yoast/YoastSEO.js/blob/develop/src/config/stopwords.js", 'r')
    stopwords = f.read().split('\n')
except IOError:
    print('Problem opening file')
else:
    f.close()
print('Sentence before stopwrods removed: \n', tokenized[51])
filtered = []
for t in tokenized:
    text = t[0]
    f_text = []
    for word in text:
        if word not in stopwords and len(word) > 2:
            f_text.append(word)
    filtered.append((f_text, t[1]))

print('\nSentence after stopwords removed: \n', filtered[51])

推荐阅读