Python 删除file.txt中的连词和用户输入中的标点符号
我想从用户输入的标点符号和连词中清除字符串。连接词存储在file.txt(Stop Word.txt)中 我已经尝试了以下代码:Python 删除file.txt中的连词和用户输入中的标点符号,python,list,file,input,punctuation,Python,List,File,Input,Punctuation,我想从用户输入的标点符号和连词中清除字符串。连接词存储在file.txt(Stop Word.txt)中 我已经尝试了以下代码: f = open("Stop Word.txt", "r") def message(userInput): punctuation = "!@#$%^&*()_+<>?:.,;/" words = userInput.lower().split() conjunction = f.read().split("\n")
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: "))
使用列表理解构建单词,并检查单词是否在连接词列表中:
f = open("Stop Word.txt", "r")
def message(userInput):
punctuation = "!@#$%^&*()_+<>?:.,;/"
words = userInput.lower().split()
conjunction = f.read().split("\n")
return [char.strip(punctuation) for char in words if char not in conjunction]
print (message("Hello, how are you? and where are you?"))
#['hello', 'how', 'are', 'you', 'where', 'are', 'you']
f=open(“Stop Word.txt”、“r”)
def消息(用户输入):
标点符号=“!@$%^&*()\+?:,;/”
words=userInput.lower().split()
连接=f.read().split(“\n”)
返回[char.strip(标点符号),如果char不连用,则返回单词中的char]
打印(信息(“你好,你好吗?你在哪里?”)
#[‘你好’、‘你好’、‘你’、‘在哪里’、‘你’]
f = open("Stop Word.txt", "r")
def message(userInput):
punctuation = "!@#$%^&*()_+<>?:.,;/"
words = userInput.lower().split()
conjunction = f.read().split("\n")
return [char.strip(punctuation) for char in words if char not in conjunction]
print (message("Hello, how are you? and where are you?"))
#['hello', 'how', 'are', 'you', 'where', 'are', 'you']