业务需求
需要将文本文件分每两行一组
jb51.txt
1:www.VeVB.COm
2:www.VeVB.COm
3:www.VeVB.COm
4:www.VeVB.COm
5:www.VeVB.COm
6:www.VeVB.COm
7:www.VeVB.COm
8:www.VeVB.COm
9:www.VeVB.COm
10:www.VeVB.COm
11:www.VeVB.COm
12:www.VeVB.COm
13:www.VeVB.COm
14:www.VeVB.COm
15:www.VeVB.COm
16:www.VeVB.COm
核心代码:
# -*- 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 jb51.txt
o.txt的内容
到这里这篇文章就完成了,希望大家以后多多支持武林网。
新闻热点
疑难解答
图片精选