编程语言
首页 > 编程语言> > python .txt文件奇数偶数行分开保存

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)

标签:__,txt,python,偶数,outfile,Wnt,outfile1,infile
来源: https://blog.csdn.net/Daisy4/article/details/123035478