python將文本分每兩行一組并保存到文件
業(yè)務需求
需要將文本文件分每兩行一組
jb51.txt
1:chabaoo.cn
2:chabaoo.cn
3:chabaoo.cn
4:chabaoo.cn
5:chabaoo.cn
6:chabaoo.cn
7:chabaoo.cn
8:chabaoo.cn
9:chabaoo.cn
10:chabaoo.cn
11:chabaoo.cn
12:chabaoo.cn
13:chabaoo.cn
14:chabaoo.cn
15:chabaoo.cn
16:chabaoo.cn
核心代碼:
# -*- 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")
通過執(zhí)行 python jb51.txt
o.txt的內容

到這里這篇文章就完成了,希望大家以后多多支持腳本之家。
相關文章
使用Py2Exe for Python3創(chuàng)建自己的exe程序示例
今天小編就為大家分享一篇使用Py2Exe for Python3創(chuàng)建自己的exe程序示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
記一次pyinstaller打包pygame項目為exe的過程(帶圖片)
這篇文章主要介紹了記一次pyinstaller打包pygame項目為exe的過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

