These two scripts can convert datasets between the LDIF (LDAP Data Interchange
Format) and CSV (Comma-Separated Values) formats, in both directions, without
losing information.
(Comments in the LDIF are not preserved; ordering of multi-value attributes is
preserved.)
./ldif2csv data.ldif > data.csv
or
./ldif2csv < data.ldif > data.csv
./csv2ldif data.csv > data.ldif
or
./csv2ldif < data.csv > data.ldif
-
The CSV files use
;as the cell separator, enclose all cells in"(with"s in the input represented as"") and separate the values of a multi-value attribute with|:"dn";"email";"objectclass" "uid=johndoe,dc=example,dc=com";"johndoe@example.com";"inetOrgPerson|top"
The
""escaping allows you to use both;and"in your data. If you need to use|, you'll have to edit theJOINERdefinition in both script files.
ldif2csvwill examine your data and output suggestions for unused characters that could be used as joiners or delimiters. -
Individual attribute values¹ longer than 499 bytes (think
jpegphotos) are split off into individual files to keep the CSV small.
They are referenced asFILE=dn=…,attr=…,id=…in the respective CSV cell. If this format is kept intact, they will be reintegrated into the LDIF bycsv2ldif.
The user is free to manually insert or modify theseFILE=…references — if they point to a valid file, they will be followed; if not, the converter will crash and burn. Enjoy!
(¹ Note that this means you can have a mixture of "direct" and "referenced" values in the same cell.)
- Conversion from CSV to LDIF is very slow when the input contains a lot of
binary data (e.g.
jpegphotoattributes). - Error handling? What's that?
If something goes wrong, you'll get your usual Python exception stack trace.