python将文本分每两行一组并保存到文件-创新互联

业务需求

创新互联建站是专业的木兰网站建设公司,木兰接单;提供网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行木兰网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

需要将文本文件分每两行一组

jb51.txt

1:www.jb51.net
2:www.jb51.net
3:www.jb51.net
4:www.jb51.net
5:www.jb51.net
6:www.jb51.net
7:www.jb51.net
8:www.jb51.net
9:www.jb51.net
10:www.jb51.net
11:www.jb51.net
12:www.jb51.net
13:www.jb51.net
14:www.jb51.net
15:www.jb51.net
16:www.jb51.net

核心代码:

# -*- coding: utf-8 -*-
'''
python读取文件,每两行为一组
'''
def fenhang(infile,outfile):

 infopen = open(infile,'r',encoding='utf-8')
 outopen = open(outfile,'w',encoding='utf-8')
 lines = infopen.readlines()
 i = 1
 for line in lines:
  if i % 2 == 0:
   outopen.write(line+'\n')
  else:
   outopen.write(line)
  i += 1
 infopen.close()
 outopen.close()
fenhang("jb51.txt","o.txt")

当前文章:python将文本分每两行一组并保存到文件-创新互联
网页地址:http://azwzsj.com/article/ccooid.html