[英]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.