python奇偶行分开存储实现代码

时间:2022-09-04 17:11:03

例子:

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

python函数代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding: utf-8 -*-
'''
python读取文件,偶数行输出一个文件,奇数行输出一个文件
'''
def fenhang(infile,outfile,outfile1):
 
 infopen = open(infile,'r',encoding='utf-8')
 outopen = open(outfile,'w',encoding='utf-8')
 outopen1 = open(outfile1, 'w', encoding='utf-8')
 lines = infopen.readlines()
 i = 0
 for line in lines:
 i += 1
 if i % 2 == 0:
  outopen.write(line)
 else:
  outopen1.write(line)
 infopen.close()
 outopen.close()
fenhang("zzvips.txt","oushu.txt","jishu.txt")

 

python中%代表什么意思

求模运算,相当于mod,也就是计算除法的余数,比如5%3就得到2。

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