Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2013, 01:47 PM
s0ke s0ke is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 s0ke User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 3 sec
Reputation Power: 0
Print info from text file

What would be the best way to query and print info from a text file using a certain keyword?

As an example if I wanted to query the aSN field of a text file and print out the username.

So an example would be if I exectued my script with 4TJW and have it return the info from user. So it would return test.

"user" "test" {
"name" = "test"
"princName" = "test@local"
"cn" = "test"
"ID" = 12345
"aSN" = "4TJW"
}

"user" "test2" {
"cn" = "test2"
"sAmname" = "test2"
"email" = "test2@corp.com"
"ID" = 422223
"aSN" = "TRI3"
}

Reply With Quote
  #2  
Old February 7th, 2013, 03:10 PM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
I may have a solution but I don't claim that would be the best way of doing this.

One possible solution for me would be the following, assuming that the syntax of the file remains the same in the future (like automatic log files generated by a program)

Code:
import re
import sys

def main():
    with open("testdata2.txt", "r") as inputFile:
        # This is the aSN code provided as the command line argument
        asnInputValue = sys.argv[1]
        
        # Each line including the user name starts with "user" followed
        # by something (that is, the actual user name), therefore the 
        # following regular expression pattern may be used in order to 
        # detect such line
        userNameProg = re.compile(r"\"user\"[ ]+\"(?P<userName>[^ \"]+)\"")
        
        # The regular expression matching the aSN code
        asnProg = re.compile(r"\"aSN\"[ ]*=[ ]*\"(?P<asnValue>[^ \"]+)\"")
        
        # start reading the file, line by line and looking for the 
        # corresponding matched regular expressions for user name and aSN.
        userName = None
        asnCode = None
        for line in inputFile:
            # user name found in the current line being processed
            userNameMatched = userNameProg.match(line)
            # aSN code  found in the current line being processed
            asnMatched = asnProg.match(line)
            
            if userNameMatched:
                userName = userNameMatched.group("userName")
            elif asnMatched:
                asnCode = asnMatched.group("asnValue")
                if asnCode == asnInputValue:
                    print ("The user name for the aSN code {0} is {1}".
                        format(asnCode, userName))
            else:
                continue
    
main()



I run this script (I use Python 3) with the following test file
Code:
"user" "test1" 
{
"name" = "test1"
"princName" = "test1@local"
"cn" = "test1"
"ID" = 12345
"aSN" = "TRI1"
}

"user" "test2" 
{
"cn" = "test2"
"sAmname" = "test2"
"email" = "test2@corp.com"
"ID" = 422223
"aSN" = "TRI2"
}

"user" "test3" 
{
"cn" = "test2"
"sAmname" = "test2"
"email" = "test2@corp.com"
"ID" = 422223
"aSN" = "TRI3"
}

"user" "test4" 
{
"cn" = "test4"
"sAmname" = "test4"
"email" = "test4@corp.com"
"ID" = 422223
"aSN" = "TRI4"
}


And here is the result that I obtain:
Code:
$ python -tt myscript.py TRI1
The user name for the aSN code TRI1 is test1

$ python -tt myscript.py TRI2
The user name for the aSN code TRI2 is test2

$ python -tt myscript.py TRI3
The user name for the aSN code TRI3 is test3

$ python -tt myscript.py TRI4
The user name for the aSN code TRI4 is test4



Regards,
Dariyoosh

Reply With Quote
  #3  
Old February 7th, 2013, 07:12 PM
s0ke s0ke is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 s0ke User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 3 sec
Reputation Power: 0
Thank you. Very nice. Could I also then use a regex to extract the cn name as well?

[edit]
nm i got. Thanks for the help on this. Very much appreciated.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Print info from text file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap