import re
filename1 = '001.txt' # txt文件和当前脚本在同一目录下,所以不用写具体路径
filename2 = '002.txt'
if __name__ == "__main__":
# 打开文件,用with代替try cach
with open(filename1, 'r') as f:
str = f.read()
# 把所有字符中的大写字母转换成小写字母
str = str.lower()
# 分隔成数组
wordList = str.split(' ')
newWord = []
for word in wordList:
# 反向正则
word = re.sub(r'[^a-z]+', '', word)
if word not in newWord and word != '':
newWord.append("+"+word)
print(newWord)
# 将数组重新粘结
c = "\n#\n&\n@\n$1\n".join(newWord)
print(c)
f.close()
f2 = open(filename2, 'w')
f2.write(c)
f2.close()
# str=compileX.findall(str) #正则方式之一
# str = re.sub(r'[^a-z]+', '', str)
python金山词霸单词本批量导入
最新推荐文章于 2024-07-20 17:12:48 发布