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

yipeiwu_com5年前Python基础

例子:

1:www.jb51.net
2:www.jb51.net
3:www.jb51.net
4:www.jb51.net
5:www.jb51.net
6:www.jb51.net
7:www.jb51.net
8:www.jb51.net
9:www.jb51.net
10:www.jb51.net
11:www.jb51.net
12:www.jb51.net
13:www.jb51.net
14:www.jb51.net
15:www.jb51.net
16:www.jb51.net

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基础之包的导入和__init__.py的介绍

调用同级目录: – src |– mod.py |– test.py 若在程序test.py中导入模块mod, 则直接使用 import mod 或 from mod im...

python PyQt5/Pyside2 按钮右击菜单实例代码

具体代码如下所述: import sys from PySide2.QtGui import * from PySide2.QtCore import * from PySide2....

python中pip的使用和修改下载源的方法

基本命令 显示版本信息 pip -V 安装指定包 pip install <packages> pip install -i 'host' <package...

python 常用的基础函数

Python: 1. print()函数:打印字符串 2. raw_input()函数:从用户键盘捕获字符 3. len()函数:计算字符长度 4. format(12.3654,'6....

python写入中英文字符串到文件的方法

本文实例讲述了python写入中英文字符串到文件的方法。分享给大家供大家参考。具体分析如下: python中如果使用系统默认的open方法打开的文件只能写入ascii吗,如果要写入中文需...