Python Forum
Looking code to convert my strings of line in Dict - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Looking code to convert my strings of line in Dict (/thread-6191.html)



Looking code to convert my strings of line in Dict - santosh - Nov-10-2017

I want to read all the file in that location by today's date & result as output I want the uid list that BIND in ldap & cn list that search against netgroup.
but as all line is not remain same I am unable to get the way to write a code. please help me if you have better ID ...
this files have thousands of line.


this is my code:
import glob
import time
import re
from collections import defaultdict
date = time.strftime("%Y%m%d")
print date
list_of_files = glob.glob('/u01/app/oracle/product/ds/ora-nis-nl/txnlogs/access.' + date + '-*[1-9]')
#print list_of_files
    for file in list_of_files:
        with open(file, 'r') as list_of_files:
            z = list_of_files.readlines()
            #print line
            for line in z:
                key, value = line.strip().split(':')
                y[key].append(value)
                print (y)
        list_of_files.close()
-----------------------------------------
Result on execution
Error:
Traceback (most recent call last): File "pulllog1.py", line 14, in <module> key, value = line.strip().split() ValueError: too many values to unpack
-------------------------------------------sample of file content-------that code is reading line by line--
Output:
[10/Nov/2017:07:26:30 +0000] conn=8150491 op=3639285 msgId=3639286 - SRCH base="ou=people,dc=example,dc=com" scope=1 filter="(&(objectClass=posixAccount)(uidNumber=-2))" attrs="userPassword cn gidNumber uidNumber loginShell objectClass gecos uid homeDirectory" [10/Nov/2017:07:26:30 +0000] conn=8150491 op=3639285 msgId=3639286 - RESULT err=0 tag=101 nentries=0 etime=0 [10/Nov/2017:07:26:30 +0000] conn=8150492 op=3642946 msgId=3642947 - SRCH base="ou=people,dc=example,dc=com" scope=1 filter="(&(objectClass=posixAccount)(uidNumber=-2))" attrs="userPassword cn gidNumber uidNumber loginShell objectClass gecos uid homeDirectory" [10/Nov/2017:07:26:30 +0000] conn=8150492 op=3642946 msgId=3642947 - RESULT err=0 tag=101 nentries=0 etime=0 [10/Nov/2017:07:26:30 +0000] conn=8150491 op=3639286 msgId=3639287 - SRCH base="ou=people,dc=example,dc=com" scope=1 filter="(&(objectClass=posixAccount)(uidNumber=-2))" attrs="userPassword cn gidNumber uidNumber loginShell objectClass gecos uid homeDirectory" [10/Nov/2017:07:26:30 +0000] conn=8150491 op=3639286 msgId=3639287 - RESULT err=0 tag=101 nentries=0 etime=0 [10/Nov/2017:07:26:30 +0000] conn=8150492 op=3642947 msgId=3642948 - SRCH base="ou=people,dc=example,dc=com" scope=1 filter="(&(objectClass=posixAccount)(uidNumber=-2))" attrs="userPassword cn gidNumber uidNumber loginShell objectClass gecos uid homeDirectory" [10/Nov/2017:07:26:30 +0000] conn=8150492 op=3642947 msgId=3642948 - RESULT err=0 tag=101 nentries=0 etime=0 [10/Nov/2017:07:26:31 +0000] conn=8150491 op=3639287 msgId=3639288 - SRCH base="ou=people,dc=example,dc=com" scope=1 filter="(&(objectClass=posixAccount)(uidNumber=-2))" attrs="userPassword cn gidNumber uidNumber loginShell objectClass gecos uid homeDirectory" [10/Nov/2017:07:26:31 +0000] conn=8150491 op=3639287 msgId=3639288 - RESULT err=0 tag=101 nentries=0 etime=0 [10/Nov/2017:07:27:31 +0000] conn=2931239 op=2054033 msgId=2054034 - RESULT err=0 tag=101 nentries=1 etime=0 [10/Nov/2017:07:27:31 +0000] conn=2931239 op=2054034 msgId=2054035 - SRCH base="ou=netgroup,dc=example,dc=com" scope=1 filter="(&(objectClass=nisNetgroup)(cn=cloud_santosh_db))" attrs="cn nisNetgroupTriple memberNisNetgroup" [10/Nov/2017:07:27:31 +0000] conn=3139057 op=1041247 msgId=1041248 - SRCH base="ou=netgroup,dc=example,dc=com" scope=1 filter="(&(objectClass=nisNetgroup)(cn=cloud_ancg_prod_arch_db))" attrs="cn nisNetgroupTriple memberNisNetgroup" [10/Nov/2017:07:28:07 +0000] conn=8860723 op=2 msgId=3 - BIND dn="uid=sftoopemm,ou=people,dc=example,dc=com" method=128 version=3



RE: Looking code to convert my strings of line in Dict - heiner55 - Nov-10-2017

Maybe this helps:

#!/usr/bin/python3
import glob
import time

date = time.strftime("%Y%m%d")
filenames = glob.glob('./access.' + date + '-*[1-9]')

for filename in filenames:
    print(filename)
    with open(filename, 'r') as file:
        for line in file:
            line = line.strip()
            print(line)
            # do something with line