我正在从事一个项目,在该项目中,我正在尝试在txt文件中找到所有唯一的单词,但是我似乎陷入了编码行后发生的错误这可以从列表中删除所有标点符号。
这是我到目前为止的代码:
import string
a = open('blank.txt')
def main():
check = []
for line in a:
lines = line.lower()
words = line.split()
for word in words:
if word not in check:
check.append(word)
check = [''.join(c for c in s if c not in string.punctuation) for s in check]
check = [s for s in x if s]
check.sort()
print(check)
if __name__ == '__main__':
main()
运行此操作后,我会收到一条错误消息:如果不在检查中:
UnboundLocalError: local variable 'check' referenced before assignment
想知道是否有人有任何帮助。谢谢
这应该与您基于现有代码实现的目标足够近:
import string
def main():
# `check` needs to be inside your function to be accessible.
# → You can read about "variable scope" if in doubt.
# You also need to read your file inside your function
# if you don't pass any argument to it.
# Note that it is generally better to use a 'with' block
# to automatically close your file when you are done with it.
check = []
with open('blank.txt') as a:
content = a.read().lower().split()
for word in content:
if word not in check:
check.append(word)
check = [''.join(c for c in s if c not in string.punctuation)
for s in check]
check = [s for s in check if s]
print(check)
if __name__ == '__main__':
main() # This has to be indented
如果blank.txt
包含以下文本:
我正在研究一个项目,我正在尝试在TXT文件中找到所有唯一单词,但是我似乎陷入了在编码从列表中删除所有标点符的行后发生的此错误。
您的输出将是:
['im', 'working', 'on', 'a', 'project', 'where', 'trying', 'to', 'find', 'all', 'the', 'unique', 'words', 'in', 'txt', 'file', 'however', 'i', 'seem', 'be', 'stuck', 'this', 'error', 'that', 'occurred', 'after', 'coded', 'line', 'removes', 'punctuation', 'from', 'list']
如果您的文件为空,您只需获取一个空列表。