1

I have a certain file which contains data similar to the given format

Name  :  Neha xxxxx
Title  :  ENGINEER.xxxxx xxxxxx
Employee number  :  27xxx
Status : Active
User ID :  nehxxx
Manager ID  :  xxxx
Manager : Krisxxxxxxxx

This data is to be sequentially inserted into a database.For that purpose , i am first building lists by the following code

filename = "LDAPFile.txt"
lines = open(filename).read().splitlines()

#print lines
for item in lines:
    if('Name') in item:
        Name = item.split(':')[1]
        #print Name[1]
    if('Title') in item:
        Title = item.split(":")[1]
        #print Title[1]
    if('Employee number') in item:
        ENO = item.split(":")[1]
        #print ENO
    if('Status') in item:
        Status = item.split(":")[1]
        #print Status
    if('User ID') in item:
        UID = item.split(":")[1]
        #print UID
    if('Manager ID') in item:
        MID = item.split(":")[1]
        #print MID
        #print len(MID)
    if('Manager') in item:
        MANAGER = item.split(":")
        print MANAGER
        #print len(MANAGER)

However , if('Manager') in item: results in both manager ID and Manager. How can I specifically search for Manager ?

2
  • 1
    Did you try if ('Manager :') in item ? that should work if you don't assume "Manager:" Commented Mar 13, 2014 at 13:49
  • @jonrsharpe: won't solve OP's problem, since "Manager ID" also starts with "Manager" (although it's better for all of the tests). Commented Mar 13, 2014 at 13:52

4 Answers 4

4

The minimal change you can make is this:

if item.startswith("Manager :"):

This will be efficient, as you don't have to search the whole string, and will avoid finding the same string elsewhere.

However, you can improve the whole code as follows:

data = {}
for item in lines:
    try:
        key, value = item.split(":")
    except ValueError:
        pass # ignore line - not in expected format
    else:
        data[key.strip()] = value.strip()

You can now access the fields within the data dictionary

data["Manager"] ...
Sign up to request clarification or add additional context in comments.

3 Comments

+1: Man, this is so farking simple an approach, I want to cry. Learned something here. :)
Excellent Suggestion. However,my data is separated by new lines. Moreover , "key, value = item.split(":")" throws the error "needs more than 1 value to unpack".
Do you mean separated by blank lines? If that is the case, you will get that error on those blank lines, as the result of the split is only one thing. I have added a try to catch these issues.
1

Use regex from re of Python to achieve this. In below example it checks for the Manager that is not followed by " ID"

if re.match("Manager(?!\s+ID)", item):

Remember, this example is only effective for your scenario.

Comments

1

Why not split the lines first:

for item in lines:
    parts = item.split(':')
    if parts[0].strip() == "Manager":
        # process the item

Comments

0

I think that it would be easier to use regular expressions. So what I would do is the following:

import re

# create a list to save the whole file in it

inf = open(filename, "r")
read = inf.readlines()
inf.close

for l in read:
    mat1 = re.search(r'Manager ID',l,re.M)
    mat1 = re.search(r'Manager ID',l,re.M)
    if mat1:
     MID = l.split(":")[1]
    elif mat2:
     Manager = l.split(":")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.