Last active
October 23, 2021 13:15
-
-
Save sayz/2bf1a23d83be653bbad01f74ec78b599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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