我试图弄清楚如何打开文件,将文件中的所有字母变成小写,然后取出所有标点符号。我已经尝试了一些我在网上和我的书中看到的东西,但我似乎无法弄清楚。
import string
def ReadFile(Filename):
try:
F = open(Filename)
F2=F.read()
except IOError:
print("Can't open file:",Filename)
return []
F3=[]
for word in F2:
F3=F2.lower()
exclude = set(string.punctuation)
F3= ''.join(ch for ch in F3 if ch not in exclude)
return F3
Name = input ('Name of file? ')
Words = ReadFile(Name)
print (F3)
给定一个句子,例如,
Then he said, "I'm so confused!".
成为
then he said im so confused