欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python将文本分每两行一组

程序员文章站 2023-09-11 11:44:51
# -*- coding: utf-8 -*- ''' python读取文件,每两行为一组 ''' def fenhan...
# -*- 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("源文件路径","目标文件路径")