1

Example, I have the following lines in file:

  1. Tom is a guy
  2. Sara is a woman
  3. Alex is a guy too

I would like to search for "Sara" but would like to return the whole like

   def findLine(self, str):
   ...

When I call findLine("Sara"), it returns "2. Sara is a woman"

How do I accomplish this with Python and Regular Expression (or other non-regular expression method)

Scott
  • 11
  • 1

6 Answers6

5

I renamed str to content see senderle's comment

def findLine(self, content, search_str):
    for line in content.splitlines()
        if search_str in line:
            return line
    #or something else because the search_str was not found
    return None

or if you want a list of all the lines containing sarah

def findLine(self, str, search_str):
    return [x for x in str.splitlines() if search_str in x]

search_str being the string you would like to find.

GWW
  • 43,129
  • 11
  • 115
  • 108
2
def findline(filename, search_string):
    with open(filename, 'r') as infile:
        for line in infile:
            if search_string in line:
                return line
        else:
            return None
senderle
  • 145,869
  • 36
  • 209
  • 233
  • You don't need `'r'` nor the `with` statement. Python's GC closes the file –  Jun 21 '11 at 02:34
  • @Franklin, `'r'` has _nothing_ to do with `with` or the GC. It determines which mode the file is [opened in](http://docs.python.org/library/functions.html#open). Mode is always optional; when it is omitted, the `'r'` is assumed. But [explicit is better than implicit](http://www.python.org/dev/peps/pep-0020/). – senderle Jun 21 '11 at 02:42
  • Every single person in the whole world knows the default way of opening files is `read`. –  Jun 21 '11 at 02:48
  • Hah, sender. I don't think Franklin meant to make it seem like the `'r'` and `with` statement were related. Just that neither of them are necessary. – Bryce Siedschlaw Jun 21 '11 at 03:06
  • @Bryce, you're right, I misunderstood Franklin's comment. Thanks! – senderle Jun 21 '11 at 03:31
  • @Franklin, sorry to have been brusque, but I still feel that both the `with` statement and the explicit `'r'` are best practices. [See here](http://stackoverflow.com/questions/1834556/does-a-file-object-automatically-close-when-its-reference-count-hits-zero) for more info on why it's not good to rely on the GC to close files. More generally, it's not good to rely on the behavior of one implementation -- not all versions of Python behave like cpython. – senderle Jun 21 '11 at 03:32
2

You don't need RegEx to do this. You could use file.readline() and check if the line contains "Sara". Return the line if it does. Use a for-each loop to check each line in a file.

EDIT:

def findlines(filename, searchterm):
    lines = []
    line = filename.readline()

    while line:
        if searchterm in line:
            lines.append(line)

        line = filename.readline()

    return lines
Brewer
  • 407
  • 1
  • 7
  • 15
1
def find(name)
    for line in open('file.txt'):
        if name in line:
            return line
JBernardo
  • 32,262
  • 10
  • 90
  • 115
0

Well, you don't need regular expressions:

def findline(search_string, file_name, offset=0):
    with open(file_name, 'r') as infile:
        infile.seek(offset)
        for line in infile
            if search_string in line:
               return line

(The above is actually just a combination of others' with the addition of file_name as a function param, and offset, meaning you can now traverse the file and get multiple incidences of your sought string).

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0
In [6]: lines="""1. Tom is a guy
2. Sara is a woman
3. Alex is a guy too"""

In [10]: lines=lines.splitlines()

In [11]: def findLine(word):
   ....:     return filter(lambda x: word in x,lines)
   ....: 

In [12]: findLine("Alex")
Out[12]: ['3. Alex is a guy too']
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116