业务需求
需要将文本文件分每两行一组
zzvips.txt
1:www.zzvips.com
2:www.zzvips.com
3:www.zzvips.com
4:www.zzvips.com
5:www.zzvips.com
6:www.zzvips.com
7:www.zzvips.com
8:www.zzvips.com
9:www.zzvips.com
10:www.zzvips.com
11:www.zzvips.com
12:www.zzvips.com
13:www.zzvips.com
14:www.zzvips.com
15:www.zzvips.com
16:www.zzvips.com
核心代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# -*- 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( "zzvips .txt" , "o.txt" ) |
通过执行 python zzvips.txt
o.txt的内容
到这里这篇文章就完成了,希望大家以后多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_32113189/article/details/79578553