概述
业务需求
需要将文本文件分每两行一组
jb51.txt
1:www.uoften.com
2:www.uoften.com
3:www.uoften.com
4:www.uoften.com
5:www.uoften.com
6:www.uoften.com
7:www.uoften.com
8:www.uoften.com
9:www.uoften.com
10:www.uoften.com
11:www.uoften.com
12:www.uoften.com
13:www.uoften.com
14:www.uoften.com
15:www.uoften.com
16:www.uoften.com
核心代码:
# -*- coding: utf-8 -*- ''' python读取文件,每两行为一组 ''' def fenhang(infile,outfile): infopen = open(infile,'r',encoding='utf-8') outopen = open(outfile,'w',encoding='utf-8') lines = infopen.readlines() i = 1 for line in lines: if i % 2 == 0: outopen.write(line+'\n') else: outopen.write(line) i += 1 infopen.close() outopen.close() fenhang("jb51.txt","o.txt")
通过执行 python jb51.txt
o.txt的内容
到这里这篇文章就完成了,希望大家以后多多支持脚本之家。
最后
以上就是深情黑猫为你收集整理的python将文本分每两行一组并保存到文件的全部内容,希望文章能够帮你解决python将文本分每两行一组并保存到文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复