我正在学习 python,我的第一个任务是将 Zimbrazmprov
格式的文件转换为csv
和ldif
.
由于我不知道 Python 内置函数来完成任务,所以我走了很长一段路,遍历行和打印。
如果你们能告诉我如何正确地做到这一点,我将不胜感激。
这是输入 zmp_file,要转换为 csv 和 ldif
ca [email protected] ''
ma [email protected] cn 'User One'
ma [email protected] cpf ''
ma [email protected] l 'Porto Alegre'
ca [email protected] ''
ma [email protected] cn 'User Two'
ma [email protected] cpf '0123456789'
ma [email protected] l ''
所需的 .csv 输出(字段顺序不重要)
mail,cn,cpf,l
[email protected],"User One",,"Porto Alegre"
[email protected],"User Two",0123456789,
以及所需的 .ldif 输出(字段的顺序并不重要)
dn: '[email protected]'
cn: 'User One'
l: 'Porto Alegre'
mail: '[email protected]'
dn: '[email protected]'
cn: 'User Two'
cpf: '0123456789'
mail: '[email protected]'
我能走多远:
with zmp_file as input_file
for line in input_file:
if line.startswith('ca'):
mail = line.split()[1]
print "dn: uid={0}".format(mail)
print "mail: {0}".format(mail)
elif line.startswith('ma'):
words = shlex.split(line)[-2:]
print "{0}: {1}".format(words[0], words[1])
else:
print
好的。知道了。
我知道这不是 codereview.stackexchange.com 但如果有人有意见,我在这里学习。
#!/usr/bin/env python
import csv
import os
import shlex
import sys
from ldif import LDIFParser, LDIFWriter
def zmp_to_csv_and_ldif(zmp_file):
all_attrs = set()
data = {}
records = {}
with zmp_file as input_file:
for line in input_file:
if line.startswith('ca'):
cmd, mail, pwd = line.split()
data['mail'] = mail
data['userpassword'] = pwd
records[mail] = data
all_attrs.update(['mail','userpassword'])
elif line.startswith('ma'):
cmd, mail, attr, value = shlex.split(line)
data[attr] = value
records[mail] = data
all_attrs.add(attr)
else:
data = {}
with open('/tmp/rag-parsed.csv', 'w') as output_file:
csv_writer = csv.DictWriter(output_file, fieldnames=all_attrs, extrasaction='ignore', lineterminator='\n')
csv_writer.writeheader()
for mail, data in sorted(records.items()):
csv_writer.writerow(data)
with open('/tmp/rag-parsed.ldif', 'w') as output_file:
b64_attrs = map(str.lower, ['jpegPhoto', 'userPassword'])
ldif_writer = LDIFWriter(output_file, base64_attrs=b64_attrs, cols=999)
for mail, data in sorted(records.items()):
dn = "uid={0}".format(mail)
data_in_ldap_fmt = dict([k, v.split('\n')] for k, v in data.items() if v)
ldif_writer.unparse(dn, data_in_ldap_fmt)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句