Python Forum
Looking code to convert my strings of line in Dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking code to convert my strings of line in Dict
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 299 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Trying to understand strings and lists of strings Konstantin23 2 753 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,973 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,646 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Splitting strings in list of strings jesse68 3 1,749 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,390 Feb-13-2022, 07:13 PM
Last Post: menator01
  convert c code python satyanarayana 5 5,566 Sep-21-2021, 07:45 PM
Last Post: deanhystad
  how long can a line of code be? Skaperen 2 2,202 Jun-09-2021, 06:31 PM
Last Post: Skaperen
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,875 Jan-22-2021, 05:43 AM
Last Post: buran
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,102 Dec-06-2020, 10:10 PM
Last Post: Aizou

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020