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

别总是抱怨生活不够幸运,是你欠生活一份努力。未来美不美,取决于你现在拼不拼!

例子:

1:www.haodaima.com
2:www.haodaima.com
3:www.haodaima.com
4:www.haodaima.com
5:www.haodaima.com
6:www.haodaima.com
7:www.haodaima.com
8:www.haodaima.com
9:www.haodaima.com
10:www.haodaima.com
11:www.haodaima.com
12:www.haodaima.com
13:www.haodaima.com
14:www.haodaima.com
15:www.haodaima.com
16:www.haodaima.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 == 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奇偶行分开存储实现代码。我能在冬天的严酷环境中生存下来,可能我会在春天是最漂亮的。更多关于python奇偶行分开存储实现代码请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战