这篇文章主要介绍了python将每个单词按空格分开并保存到文件中,需要的朋友可以参考下
核心代码
# -*- coding: utf-8 -*- ''' python读取英文文件,将每个单词按照空格分开,并将每个单词独自放一行 ''' def dcfenhang(infile,outfile): infopen = open(infile,'r',encoding='utf-8') outopen = open(outfile,'w'<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>,encoding='utf-8') lines = infopen.readlines() for line in lines: for db in line.split(): if db not in outfile: outopen.write(db+'\n') infopen.close() outopen.close() dcfenhang("jb51.txt","fenci.txt")
jb51.txt文件
welcome to visit gaodaima.com
Thans very much
效果如下图所示
这篇文章就介绍到这了,希望大家以后多多支持gaodaima搞代码网。
以上就是python将每个单词按空格分开并保存到文件中的详细内容,更多请关注gaodaima搞代码网其它相关文章!