python - 我该如何解决这个问题“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]
解决方案
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])
推荐阅读
- lua - lua 代码工作,但在代码完成之前停止
- ios - 有没有免费的方法可以使用 Phonegap 为 iOS 设备进行开发?
- flutter - Flutter:升级androidcompiledSdkVersion
- python - 如何使用小部件列表
- python - 如何在读取多个文件时提供编码?
- java - 具有 @DiscriminatorValue 的多个对象的单个存储库
- c++ - CMake 链接说明
- java - 使用自定义标签将 struts 1.3 迁移到 spring - 需要帮助
- php - 基于 JSON AJAX 复选框选择更新 SQL 'WHERE'
- apache-spark - Spark History 服务器未列出已完成的 jar