Results 1 to 10 of 10

Thread: Read output of file and search for a string, python

  1. #1
    Join Date
    Oct 2011
    Beans
    9

    Read output of file and search for a string, python

    Hey everyone,

    I'm very new in Python. I have to read output of the file, search for the string and read the information in that line.

    H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026

    Line look like that:

    The search string is
    H 1 6563A the information I need is in bold.

    Please help

  2. #2
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: [SOLVED] Python: search file like grep

    You should probably start a new thread...but anyway:

    Code:
    for line in open("file"):
     if "H 1 6563A" in line:
      split_string = line.split()
      result_string = split_string[3] + " " + split_string[4] 
      print result_string
    Correct me if I'm wrong.

  3. #3
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: [SOLVED] Python: search file like grep

    Quote Originally Posted by MG&TL View Post
    You should probably start a new thread...
    You are right.

    But since Yukawa didn't, I have

  4. #4
    Join Date
    Oct 2011
    Beans
    9

    Re: [SOLVED] Python: search file like grep

    Quote Originally Posted by MG&TL View Post
    You should probably start a new thread...but anyway:

    Code:
    for line in open("file"):
     if "H 1 6563A" in line:
      split_string = line.split()
      result_string = split_string[3] + " " + split_string[4] 
      print result_string
    Correct me if I'm wrong.
    Thank you for such a quick answer. This works jut fine when string is at the beginning of the line, but sometimes is in the middle? What to do in that case?

  5. #5
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Read output of file and search for a string, python

    So, do you mean the two 'words' after "6563A" anywhere in the string. If so:

    Code:
    import string
    
    line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
    searchstring = "6563A"
    
    split_string = line[string.find(line,searchstring):].split()
    
    result_string = split_string[1] + " " + split_string[2]
    print result_string
    with this result:

    Code:
    sander@R540:~$ python stringding.py 
    40.092 3.1917
    sander@R540:~$

  6. #6
    Join Date
    Oct 2011
    Beans
    9

    Re: Read output of file and search for a string, python

    Quote Originally Posted by sanderj View Post
    So, do you mean the two 'words' after "6563A" anywhere in the string. If so:

    Code:
    import string
    
    line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
    searchstring = "6563A"
    
    split_string = line[string.find(line,searchstring):].split()
    
    result_string = split_string[1] + " " + split_string[2]
    print result_string
    with this result:

    Code:
    sander@R540:~$ python stringding.py 
    40.092 3.1917
    sander@R540:~$
    Thanks mate it is working now

  7. #7
    Join Date
    Oct 2011
    Beans
    9

    Re: Read output of file and search for a string, python

    OK another question. If I wonna perform that in multiple files, with same extension. How I can do that?

  8. #8
    Join Date
    Jun 2011
    Location
    United Kingdom
    Beans
    Hidden!
    Distro
    Lubuntu Development Release

    Re: Read output of file and search for a string, python

    Using glob.glob would be one solution. http://docs.python.org/library/glob.html#glob.glob

    Code:
    for files in glob.glob('/path/to/files/*.txt'):
     for lines in open(files):
     #Do stuff with lines here.
    EDIT: although yeah, a quick google got that.
    Last edited by MG&TL; May 28th, 2012 at 10:54 PM.

  9. #9
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,485

    Re: Read output of file and search for a string, python

    Quote Originally Posted by Yukawa View Post
    OK another question. If I wonna perform that in multiple files, with same extension. How I can do that?
    ... I would advice you to read something on Python. It now feels like we're doing your homework.

  10. #10
    Join Date
    Oct 2011
    Beans
    9

    Re: Read output of file and search for a string, python

    Quote Originally Posted by sanderj View Post
    So, do you mean the two 'words' after "6563A" anywhere in the string. If so:

    Code:
    import string
    
    line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
    searchstring = "6563A"
    
    split_string = line[string.find(line,searchstring):].split()
    
    result_string = split_string[1] + " " + split_string[2]
    print result_string
    with this result:

    Code:
    sander@R540:~$ python stringding.py 
    40.092 3.1917
    sander@R540:~$
    Quote Originally Posted by MG&TL View Post
    Using glob.glob would be one solution. http://docs.python.org/library/glob.html#glob.glob

    Code:
    for files in glob.glob('/path/to/files/*.txt'):
     for lines in open(files):
     #Do stuff with lines here.
    EDIT: although yeah, a quick google got that.
    I was doing that, found it on the google, but I was having some other mistake in the code, so when i tried this in the first place it didn't work. Don't worry this is not homework, no marks no grades.

    Thanks once more.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •