当前位置:首页 >> 脚本专栏

python将每个单词按空格分开并保存到文件中

核心代码

# -*- coding: utf-8 -*-
'''
python读取英文文件,将每个单词按照空格分开,并将每个单词独自放一行
'''
def dcfenhang(infile,outfile):

  infopen = open(infile,'r',encoding='utf-8')
  outopen = open(outfile,'w',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 jb51.net
Thans very much

效果如下图所示

python将每个单词按空格分开并保存到文件中

这篇文章就介绍到这了,希望大家以后多多支持。