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

时间:2022-08-23 16:11:16

核心代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- 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("zzvips.txt","fenci.txt")

zzvips.txt文件

welcome to visit www.zzvips.com
Thans very much

效果如下图所示

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

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

原文链接:http://blog.csdn.net/qq_32113189/article/details/79591347