Skip to content

Instantly share code, notes, and snippets.

@sayz
Last active October 23, 2021 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sayz/2bf1a23d83be653bbad01f74ec78b599 to your computer and use it in GitHub Desktop.
Save sayz/2bf1a23d83be653bbad01f74ec78b599 to your computer and use it in GitHub Desktop.
import csv
#! /usr/bin/env python3
#title :test_ldap_data.py
#description :read from ldif, write as csv
#author :Sefa Yıldız
#mail :XXXX
#date :01-04-2020
#==============================================================================
#ldif'i oku ve listeye at
with open('test_data_3.txt') as f:
fil = f.readlines()
# satırların sonundaki '\n' karakterlerini sil
fi = [x.strip() for x in fil ]
#listeyi oku ve csv dosyasına uygun formatta yaz
row = 0
while row < len(fi):
csv_row = []
if 'mail' in fi[row]:
csv_row.append(fi[row][6:])
if 'accountid' in fi[row + 1]:
csv_row.append(fi[row + 1][11:])
csv_row.append(fi[row + 2][5:])
row = row + 3
else:
csv_row.append('')
csv_row.append(fi[row + 1][5:])
row = row + 1
elif 'accountid' in fi[row]:
csv_row.append('')
csv_row.append(fi[row][11:])
csv_row.append(fi[row + 1][5:])
row = row + 2
else:
csv_row.append('')
csv_row.append('')
csv_row.append(fi[row][5:])
row = row + 1
csv_row.reverse()
print(csv_row)
with open('test_data.csv', mode='a') as f:
wr = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
wr.writerow(csv_row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment