Python 如何使此程序忽略标点符号
我是python新手,我不知道如何让这个程序忽略标点符号;我知道这真的很低效,但我现在并不为此烦恼Python 如何使此程序忽略标点符号,python,Python,我是python新手,我不知道如何让这个程序忽略标点符号;我知道这真的很低效,但我现在并不为此烦恼 while True: y="y" n="n" Sentence=input("Please enter your sentence: ").upper() print("Your sentence is:",Sentence) Correct=input("Is your sentence correct? y/n ") if Correct==n: break elif Correc
while True:
y="y"
n="n"
Sentence=input("Please enter your sentence: ").upper()
print("Your sentence is:",Sentence)
Correct=input("Is your sentence correct? y/n ")
if Correct==n:
break
elif Correct==y:
Location=0
SplitSentence = Sentence.split(" ")
for word in SplitSentence:
locals()[word] = Location
Location+=1
print("")
FindWord=input("What word would you like to search? ").upper()
if FindWord not in SplitSentence:
print("Your chosen word is not in your sentence")
else:
iterate=0
WordBank=[]
for word in SplitSentence:
iterate=iterate+1
if word == FindWord:
WordBank.append(iterate)
print(FindWord, WordBank)
break
非常感谢您能给我的任何帮助。您可以使用Python的
字符串模块帮助测试标点符号
>> import string
>> print string.punctuation
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
>> sentence = "I am a sentence, and; I haven't been punctuated well.!"
请注意,“have not”中的撇号已被删除,这是完全忽略标点符号的副作用。您只想删除所有内容,而不是:a-zA-Z0-9,或者您是否设置了要测试的标点符号?
>> cleaned_sentence = ''.join([c for c in sentence if c not in string.punctuation])
>> print cleaned_sentence
'I am a sentence and I havent been punctuated well'