简体   繁体   English

如何使用python将ldif拆分为较小的文件?

[英]how to split ldif in smaller files using python?

Given a ldif file with 999 of records to be updated with ldapmodify , how can i split it in smaller files?给定一个包含 999 条记录的ldif文件要使用ldapmodify更新,我如何将其拆分为较小的文件?


https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/

Executing splitLdifByFiles('/tmp/bigfile.ldif', 10) will create 10 files and evenly distribute all records between them in a round robin manner.执行splitLdifByFiles('/tmp/bigfile.ldif', 10)将创建 10 个文件并以循环方式在它们之间均匀分布所有记录。

def splitLdifByFiles(ldiffile, nfiles):
    '''
    Recebe um arquivo ldif <ldiffile> e separa em N arquivos <nfiles> menores (round robin)
    '''

    with open(ldiffile, 'r') as fr:
        reader = fr.read()
        chunks = ['\n'+c+'\n' for c in reader.split('\n\n') if c]

    files = [open('/tmp/df.chunk.%d.ldif' % i, 'w') for i in range(nfiles)]

    for idx, chk in enumerate(chunks):
        files[idx % nfiles].writelines(chk)

    for f in files:
        f.close()

Executing splitLdifByRecords('/tmp/bigfile.ldif', 100) will write 100 records to the output file and close it, repeating this process until it reaches the last chunk of data.执行splitLdifByRecords('/tmp/bigfile.ldif', 100)会将 100 条记录写入输出文件并关闭它,重复此过程直到到达最后一块数据。

In this example, the last file will contain the remaining 99 records.在本例中,最后一个文件将包含剩余的 99 条记录。

def splitLdifByRecords(ldiffile, maxrecords):
    '''
    Recebe um arquivo ldif <ldiffile> e separa em N registros <maxrecords> por arquivo
    '''

    with open(ldiffile, 'r') as fr:
        reader = fr.read()
        chunks = ['\n'+c+'\n' for c in reader.split('\n\n') if c]

    chunkfile = None

    for idx, chk in enumerate(chunks):
        if idx % maxrecords == 0:
            if chunkfile: chunkfile.close()
            chunkfile = open('/tmp/df.chunk.%d.ldif' % (idx + maxrecords), 'w')
        chunkfile.writelines(chk)

    if chunkfile: chunkfile.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 将文件拆分为多个较小的文件 - Python Split files into multiple smaller files 将 Python 中的 blf 文件拆分成更小的文件 - Split blf-files in Python into smaller files 如何按字母顺序将大文件拆分成较小的文件? - How to split a huge file into smaller files alphabetically? 根据 python 中的唯一值将 2 个 csv 文件拆分为较小的文件集 - Split 2 csv files into smaller sets of files based on unique values in python 使用LDIF和Python删除LDAP中的属性 - Delete an attribute in LDAP using LDIF with Python 如何将列表拆分为较小的列表python - How to split a list into smaller lists python 如何拆分 csv 文件,将其标题保存在 Python 中的每个较小文件中? - How to split csv file keeping its header in each smaller files in Python? 我如何通过多处理 python 将大型 json 文件拆分为较小的 json 文件 - How i can split large json file into smaller json files through multiprocessing python Python3如何根据行内容将大文本文件拆分为较小的文件 - Python3 How to split a large text file into smaller files based on line content Python 3.x - 如何有效地将对象数组拆分为较小的批处理文件? - Python 3.x - How to efficiently split an array of objects into smaller batch files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM