find string in file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fminervino@gmail.com

    find string in file

    Hi friends !!

    I'm neophite about python, my target is to create a programa that
    find a specific string in text file.
    How can do it?

    Thanks
    fel
  • kaerbuhez

    #2
    Re: find string in file

    On 14 mar, 14:25, fminerv...@gmai l.com wrote:
    Hi friends !!
    >
    I'm neophite about python, my target is to create a programa that
    find a specific string in text file.
    How can do it?
    >
    Thanks
    fel
    $ cat text.txt
    aaa
    bbb
    ccc
    ddd
    aaa bbb
    ccc ddd
    aaa bbb ccc ddd
    aaa eee
    bbb eee
    ccc eee
    ddd eee
    $ cat bin/find_string.py
    import sys
    file_name=sys.a rgv[1]
    searched_string =sys.argv[2]
    result= [(line_number+1, line) for line_number, line in
    enumerate(open( file_name)) if searched_string in line]
    print result
    $ python bin/find_string.py text.txt eee
    [(8, 'aaa eee\n'), (9, 'bbb eee\n'), (10, 'ccc eee\n'), (11, 'ddd eee
    \n')]
    $ python bin/find_string.py text.txt aaa
    [(1, 'aaa\n'), (5, 'aaa bbb\n'), (7, 'aaa bbb ccc ddd\n'), (8, 'aaa eee
    \n')]
    $ python bin/find_string.py text.txt ddd
    [(4, 'ddd\n'), (6, 'ccc ddd\n'), (7, 'aaa bbb ccc ddd\n'), (11, 'ddd
    eee\n')]
    $

    Comment

    • Tim Chase

      #3
      Re: find string in file

      I'm neophite about python, my target is to create a programa that
      find a specific string in text file.
      How can do it?
      >>FNAME1 = 'has.txt'
      >>FNAME2 = 'doesnt_have.tx t'
      >>TEXT = 'thing to search for'
      >>TEXT in file(FNAME1).re ad()
      True
      >>TEXT in file(FNAME2).re ad()
      False

      or that may not be what your teacher wants, but if you give the
      homework problem and what you've tried, we might be able to give
      you some more quality guidance. :)

      The above is *really* *bad* code...horribly suboptimal especially
      as files get large. It only returns a boolean value. You might
      prefer to enumerate the file's contents and, if any line contains
      the search-text, return that line offset without reading the rest
      of the file.

      -tkc



      Comment

      • Roman Dodin

        #4
        Re: find string in file



        On 14 mar, 14:25, fminerv...@gmai l.com wrote:
        >Hi friends !!
        >>
        >I'm neophite about python, my target is to create a programa that
        >find a specific string in text file.
        >How can do it?
        >>
        >Thanks
        >fel
        >>
        >
        >
        >
        One more way to do this

        f = codecs.open("lo g.txt", 'r', "utf_16_le" )
        lines = f.readlines()
        for line in lines:
        if(line.find("m y string to find")!=-1):
        print line

        Comment

        • eMko

          #5
          Re: find string in file

          An of course, you can use a regular expression. (Module "re").

          Comment

          • Lie

            #6
            Re: find string in file

            On Mar 14, 8:25 pm, fminerv...@gmai l.com wrote:
            Hi friends !!
            >
            I'm neophite about python, my target is to create a programa that
            find  a specific string in text file.
            How can do it?
            >
            Thanks
            fel
            I'd recommend regular expression (import re) if the string you're
            searching is in a complex format (like all strings that starts with a
            dollar sign followed by an arbitrary number of numbers which might or
            might not contain commas every three number).

            If what you're searching is just simple queries (like searching where
            "Hello" appeared in the text you'd better use the string modules. as
            they're simpler to use and faster.

            Comment

            Working...