-1

First timer here with really using files and I/O. I'm running my code through a tester and the tester calls the different files I'm working with through my code. So for this, I'm representing the file as "filename" below and the string I'm looking for in that file as "s". I'm pretty sure I'm going through the lines of the code and searching for the string correctly. This is what I have for that :

def locate(filename, s):

    file= open(filename)
    line= file.readlines()
    for s in line:
        if s in line:
            return [line.count]

I'm aware the return line isn't correct. How would I return the number of the line that the string I'm looking for is located on as a list?

Nick
  • 97
  • 3
  • 11

3 Answers3

2

You can use enumerate to keep track of the line number:

def locate(filename, s):
    with open(filename) as f:
        return [i for i, line in enumerate(f, 1) if s in line]

In case the searched string can be found from first and third line it will produce following output:

[1, 3]
niemmi
  • 17,113
  • 7
  • 35
  • 42
  • Did you mean to return `[i]`? I think you just want `i`? – idjaw Apr 10 '16 at 01:54
  • Question states that return value should be a list: "return the number of the line that the string I'm looking for is located on as a list". Of course if integer is preferred then it should be `i` as you suggested. – niemmi Apr 10 '16 at 01:56
  • Ah! I think it is item and index together. – idjaw Apr 10 '16 at 01:57
  • 1
    Could be, difficult to tell since there's no example of preferred output. Line number & line contents would obviously be `return [i, line]`. – niemmi Apr 10 '16 at 01:59
  • 1
    Conclusion. Unclear :P – idjaw Apr 10 '16 at 01:59
  • Sorry for being unclear about the output. My bad. The examples for outputs that i'm looking for is, for example is the word I'm looking for is on line 1 and 3 the output would be : [1,3]. If on lines 1 , 2 and 3: [1,2,3] – Nick Apr 10 '16 at 02:04
  • @Nick Updated On my answer. – Jason Apr 10 '16 at 02:06
  • @Nick Thanks for clarification, I've updated my example accordingly – niemmi Apr 10 '16 at 02:09
0

You can use enumerate.

Sample Text File

hello hey s hi
hola
s

Code

def locate(filename, letter_to_find):

    locations = []
    with open(filename, 'r') as f:
        for line_num, line in enumerate(f):
            for word in line.split(' '):
                if letter_to_find in word:
                    locations.append(line_num)
    return locations

Output

[0, 2]

As we can see it shows that the string s on lines 0 and 2.
Note computers start counting at 0

Whats going on

  1. Opens the file with read permissions.

  2. Iterates over each line, enumerateing them as it goes, and keeping track of the line number in line_num.

  3. Iterates over each word in the line.

  4. If the letter_to_find that you passed into the function is in word, it appends the line_num to locations.

  5. return locations

Jason
  • 2,278
  • 2
  • 17
  • 25
0

These are the problem lines

for s in line:
    if s in line:

you have to read line into another variable apart from s

def locate(filename, s):

    file= open(filename)
    line= file.readlines()
    index = 0;
    for l in line:
        print l;
        index = index + 1
        if s in l:
            return index


print locate("/Temp/s.txt","s")
Jason
  • 2,278
  • 2
  • 17
  • 25
Sanj
  • 3,879
  • 2
  • 23
  • 26