0

I have a file in the following format

Summary;None;Description;Emails\nDarlene\nGregory Murphy\nDr. Ingram\n;DateStart;20100615T111500;DateEnd;20100615T121500;Time;20100805T084547Z
Summary;Presence tech in smart energy management;Description;;DateStart;20100628T130000;DateEnd;20100628T133000;Time;20100628T055408Z
Summary;meeting;Description;None;DateStart;20100629T110000;DateEnd;20100629T120000;Time;20100805T084547Z
Summary;meeting;Description;None;DateStart;20100630T090000;DateEnd;20100630T100000;Time;20100805T084547Z
Summary;Balaji Viswanath: Meeting;Description;None;DateStart;20100712T140000;DateEnd;20100712T143000;Time;20100805T084547Z
Summary;Government Industry Training:  How Smart is Your City - The Smarter City Assessment Tool\nUS Call-In Information:  1-866-803-2143\,     International Number:  1-210-795-1098\,     International Toll-free Numbers:  See below\,     Passcode:  6785765\nPresentation Link - Copy and paste URL into web browser:  http://w3.tap.ibm.com/medialibrary/media_view?id=87408;Description;International Toll-free Numbers link - Copy and paste this URL into your web browser:\n\nhttps://w3-03.sso.ibm.com/sales/support/ShowDoc.wss?docid=NS010BBUN-7P4TZU&infotype=SK&infosubtype=N0&node=clientset\,IA%7Cindustries\,Y&ftext=&sort=date&showDetails=false&hitsize=25&offset=0&campaign=#International_Call-in_Numbers;DateStart;20100811T203000;DateEnd;20100811T213000;Time;20100805T084547Z

Now I need to create a function that does the following:

The function argument would specify which line to read, and let say i have already done line.split(;)

  1. See if there is "meeting" or "call in number" anywhere in line[1], and see if there is "meeting" or "call in number" anywhere in line[2]. If either of both of these are true, the function should return "call-in meeting". Else it should return "None Inferred".

Thanks in advance

2
  • I'm seeing a few \n's. Are those supposed to be new lines? And you say you've already done line.split(;); would that count as separate lines for you? It might be easier to search for your text if you do the search before you split it up so you keep the same lines. Commented Aug 9, 2010 at 6:10
  • And a second question: what do you mean by line[1] and line[2]? Commented Aug 9, 2010 at 6:16

3 Answers 3

1

use the in operator to see if there is a match

for line in open("file"):
    if "string" in line :
        ....
Sign up to request clarification or add additional context in comments.

Comments

1

vlad003 is right: if you have newline characters in the lines; they will be new lines! In this case, I would split on "Summary" instead:

import itertools

def chunks( filePath ):
    "Since you have newline characters in each section,\
    you can't read each line in turn. This function reads\
    lines of the file and splits them into chunks, restarting\
    each time 'Summary' starts a line."
    with open( filePath ) as theFile:
        chunk = [ ]
        for line in theFile:
            if line.startswith( "Summary" ):
                if chunk: yield chunk
                chunk = [ line ]
            else:
                chunk.append( line )
        yield chunk

def nth(iterable, n, default=None):
    "Gets the nth element of an iterator."
    return next(islice(iterable, n, None), default)

def getStatus( chunkNum ):
    "Get the nth chunk of the file, split it by ";", and return the result."
    chunk = nth( chunks, chunkNum, "" ).split( ";" )
    if not chunk[ 0 ]:
        raise SomeError # could not get the right chunk
    if "meeting" in chunk[ 1 ].lower() or "call in number" in chunk[ 1 ].lower():
        return "call-in meeting"
    else:
        return "None Inferred"

Note that this is silly if you plan to read all the chunks of the file, since it opens the file and reads through it once per query. If you plan to do this often, it would be worth parsing it into a better data format (e.g. an array of statuses). This would require one pass through the file, and give you much better lookups.

Comments

1

A build on ghostdog74's answer:

def finder(line):
    '''Takes line number as argument. First line is number 0.'''
    with open('/home/vlad/Desktop/file.txt') as f:
        lines = f.read().split('Summary')[1:]
        searchLine = lines[line]
        if 'meeting' in searchLine.lower() or 'call in number' in searchLine.lower():
            return 'call-in meeting'
        else:
            return 'None Inferred'

I don't quite understand what you meant by line[1] and line[2] so this is the best I could do.

EDIT: Fixed the problem with the \n's. I figure since you're searching for the meeting and call in number you don't need the Summary so I used it to split the lines.

4 Comments

Assuming the OP has stated his/her problem correctly (which is a big assumption!), this won't work: the line numbers will be off, because e.g. Darlene will be on a separate line.
I fixed it. It now splits the lines by the Summary; assuming he doesn't need that word in there for searching.
you want to change the and to an or in the line that begins with if 'meeting' in searchLine.lower()
thanks very much , sorry for not being clear. Actually i had not notices the /n's inside a line!

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.