我正在尝试创建一组单词,这意味着严格意义上只从.txt文件的字母表。此txt文件包含所有可能的字符,包括不可打印的文本。在
没有re或集合库。Python3
例如,给定一个.txt文件
*eBooks$ Readable By Both Humans and By Computers, Since 1971**
*These# eBooks@ Were Prepared By Thousands of Volunteers!
我需要我的设备来容纳
^{pr2}$这是我所做的,但我仍然得到一些特殊的字符和数字在我的集合。我只想要字母表
import string
filecontent = []
word_set = {}
with open ("small.txt") as myFile:
for line in myFile:
line = line.rstrip()
line = line.replace("\t","")
for character in line:
if character in string.digits or character in string.punctuation:
line = line.replace(character, "")
if line != "":
filecontent.append(line)
lowerCase = [x.lower() for x in filecontent]
word_set = {word for line in lowerCase for word in line.split()}
你可以这样做:
如果您使用的是python3,那么您需要将
^{pr2}$str.translate
部分替换为:下面是一个使用
re
regex模块的解决方案。它还提供了一个字数,但如果你不想,你可以只使用这些键,或换成一组。在或者,如果你有一个文件
^{pr2}$这给了
如果我是你,我已经习惯了关于芬德尔在
输出
^{pr2}$相关问题 更多 >
编程相关推荐