【发布时间】:2017-03-06 03:06:53
【问题描述】:
所以我有一个大文本文件(一本书),但我试图去除整个文本文件中的标点符号、特殊字符和空格,以便形成一个包含所有单词的字典。出于某种原因,当我使用 .strip() 方法时,它实际上什么也没做。
with open(filename, 'r') as file:
entire = file.read()
entire = entire.lower() #lower case the entire text (this works)
entire = entire.strip(string.punctuations + string.digit) #this however does nothing
我如何去掉整本书的标点符号和数字,以便建立字典?
【问题讨论】:
-
因为它不应该那样做。为什么你认为它应该这样做?您不会找到任何声称此类内容的教程或文档。
-
刚开始用python编程,所以对我来说有点陌生,希望您能深入了解如何解决这个问题!干杯! :)
-
我投票决定将此问题作为离题结束,因为 SO proper 不是文档网站。
-
string.punctuations + string.digit应该是string.punctuation + string.digits(不是那条线会做你想做的事)
标签: python python-3.x file dictionary strip