【发布时间】:2020-02-11 16:26:49
【问题描述】:
我想从标点符号和连词中清除用户输入的字符串。连词存储在file.txt (Stop Word.txt)中
我已经尝试过这段代码:
f = open("Stop Word.txt", "r")
def message(userInput):
punctuation = "!@#$%^&*()_+<>?:.,;/"
words = userInput.lower().split()
conjunction = f.read().split("\n")
for char in words:
punc = char.strip(punctuation)
if punc in conjunction:
words.remove(punc)
print(words)
message(input("Pesan: "))
输出
when i input "Hello, how are you? and where are you?"
i expect the output is [hello,how,are,you,where,are,you]
but the output is [hello,how,are,you?,where,are,you?]
or [hello,how,are,you?,and,where,are,you?]
【问题讨论】:
标签: python list file input punctuation