首页 > 编程 > Python > 正文

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

2020-01-04 15:36:05
字体:
来源:转载
供稿:网友

例子:

1:www.vevb.com
2:www.vevb.com
3:www.vevb.com
4:www.vevb.com
5:www.vevb.com
6:www.vevb.com
7:www.vevb.com
8:www.vevb.com
9:www.vevb.com
10:www.vevb.com
11:www.vevb.com
12:www.vevb.com
13:www.vevb.com
14:www.vevb.com
15:www.vevb.com
16:www.vevb.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,奇偶行

python中%代表什么意思

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


注:相关教程知识阅读请移步到python教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表