python .txt文件奇数偶数行分开保存

2022/2/20 21:00:41

本文主要是介绍python .txt文件奇数偶数行分开保存,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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()

if __name__=="__main__":
    infile='/Wnt/Wnt_proteinB.txt'
    outfile='/Wnt/Wnt_proteinBBBB.txt'  ####偶数行
    outfile1='/Wnt_proteinnameB.txt'    ####奇数行
    
    fenhang(infile,outfile,outfile1)



这篇关于python .txt文件奇数偶数行分开保存的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程