Posted in Python onMarch 19, 2018
业务需求
需要将文本文件分每两行一组
3water.txt
1:3water.com
2:3water.com
3:3water.com
4:3water.com
5:3water.com
6:3water.com
7:3water.com
8:3water.com
9:3water.com
10:3water.com
11:3water.com
12:3water.com
13:3water.com
14:3water.com
15:3water.com
16:3water.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("3water.txt","o.txt")
通过执行 python 3water.txt
o.txt的内容
到这里这篇文章就完成了,希望大家以后多多支持三水点靠木。
python将文本分每两行一组并保存到文件
- Author -
为援不可图声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@