Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除file.txt中的连词和用户输入中的标点符号_Python_List_File_Input_Punctuation - Fatal编程技术网

Python 删除file.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")

我想从用户输入的标点符号和连词中清除字符串。连接词存储在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: "))

使用列表理解构建单词,并检查单词是否在连接词列表中:

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']