Python将文件拆分为多个较小的文件 [英] Python Split files into multiple smaller files

查看:61
本文介绍了Python将文件拆分为多个较小的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个名为 file_split(filename,number_of_files)的函数,该函数会将输入文件拆分为多个输出文件.文件应尽可能均匀地分割.当文件长度被要创建的文件数均分时(一个10行的文件,分成2个文件,每个输出文件应该有5行.当长度不均分时,所有输出文件的长度都不能有差异大于1.例如,一个10行的文件(一分为三)将具有长度为3、3和4的输出文件.

Write a function named file_split(filename, number_of_files) that will split an input file into a number of output files. The files should be split as evenly as possible. When the file length is evenly divisible by the number of files to create (a 10-line file, split into 2 files, each output file should have 5 lines. When the length is not evenly divisible all output files’ length must not have a difference greater than 1. For example, a file of 10 lines, split in 3, would have output files of length 3, 3 and 4.

我已经编写了代码,但是我不知道如何处理大于1的部分,我需要帮助修改我的代码以包括该部分.(如果代码不是偶数,则我为最后一行创建一个新文件)

I have written my code but I can not figure out how to do the difference greater than 1 part, I need help modifying my code to include that part. (The code I have creates a new file for the last line if it is not an even multiple)

def get_line_counts(filename, number_of_files):
    try:
        my_file = open(filename, 'r')
    except IOError:
        print("File does not exist")
        return    
    input = my_file.read().split('\n')
    outputBase = 'lel'    
    total_lines = 0
    with open('myfile.txt') as infp:
        for line in infp:
            if line.strip():  
                total_lines +=1    
    base_size = total_lines // number_of_files    
    at = 1
    for lines in range(0, len(input), base_size):
        outputData = input[lines:lines+base_size]
        output = open(outputBase + str(at) + '.txt', 'w')
        output.write('\n'.join(outputData))
        output.close()
        at += 1

推荐答案

轮循机制很容易而且很容易:

Round-robin works and is easy:

with open('myfile.txt') as infp:
    files = [open('%d.txt' % i, 'w') for i in range(number_of_files)]
    for i, line in enumerate(infp):
        files[i % number_of_files].write(line)
    for f in files:
        f.close()

这篇关于Python将文件拆分为多个较小的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆