这篇文章主要介绍了python读取文件,偶数行输出一个文件,奇数行输出一个文件,需要的朋友可以参考下
例子:
1:www.gaodaima.com
2:www.gaodaima.com
3:www.gaodaima.com
4:www.gaodaima.com
5:www.gaodaima.com
6:www.gaodaima.com
7:www.gaodaima.com
8:www.gaodaima.com
9:www.gaodaima.com
10:www.gaodaima.com
11:www.gaodaima.com
12:www.gaodaima.com
13:www.gaodaima.com
14:www.gaodaima.com
15:www.gaodaima.com
16:www.gaodaima.com
python函数代码
# -*- 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 <div style="color:transparent">来源gaodai.ma#com搞#代!码网</div>== 0: outopen.write(line) else: outopen1.write(line) infopen.close() outopen.close() fenhang("jb51.txt","oushu.txt","jishu.txt")
效果图
python中%代表什么意思
求模运算,相当于mod,也就是计算除法的余数,比如5%3就得到2。
以上就是python奇偶行分开存储实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!