0

I have a file (test.txt) like this:

[06/Oct/2017:20:18:47 +0200] [pool-2-thread-7] http://10.12.214.20
[06/Oct/2017:20:19:38 +0200] [pool-2-thread-4] http://10.12.214.18
[06/Oct/2017:20:19:38 +0200] [pool-2-thread-4] http://10.12.214.18
[06/Oct/2017:20:19:49 +0200] [pool-2-thread-8] http://10.12.214.18
[06/Oct/2017:20:19:59 +0200] [pool-2-thread-7] ends.
[06/Oct/2017:20:20:47 +0200] [pool-2-thread-7] http://10.12.214.20
[06/Oct/2017:20:21:24 +0200] [pool-2-thread-4] ends.
[06/Oct/2017:20:21:34 +0200] [pool-2-thread-8] ends.

As a python-beginner i wanted to get the linenumber of a specific line, so i searched for a solution and found this post: Finding the index of an item given a list containing it in Python. I am using the answer of @tanzil, because later on it is possible, that i do look for elements not in the list.

This is what i do:

Save the lines of the original file in a list:

with open(test.txt) as f:
    content = [line.rstrip('\n') for line in f]
    print len(content)
index = [x for x in range(len(content))]

Iterate through the original file:

with open(test.txt, "r") as file:
    for line in file:
        print find_element_in_list(index, line.rstrip('\n'))

Find the index of each line of the original file:

def find_element_in_list(index_list, list_element):
    try:
        index_element = index_list.index(list_element)
        return index_element
    except ValueError:
        return None

As a result, there is no match at all. I only get "None" as output.

I read this post 'is' operator behaves differently when comparing strings with spaces and hoped to get an answer to my question, but couldn't adapt the answers to my problem.

I would apreciate any hints or ideas!

(This is not a dublicate of Get Line Number of certain phrase in file Python because i need to access the indices of different lines multiple times. I am not iterating through the file everytime i need a specific one.)

sunjazz
  • 175
  • 1
  • 8
  • Possible duplicate of [Get Line Number of certain phrase in file Python](https://stackoverflow.com/questions/3961265/get-line-number-of-certain-phrase-in-file-python) – Elis Byberi Oct 27 '17 at 13:55
  • @sunjazz Try printing the exception. It will help you debug the problem. – akhilsp Oct 27 '17 at 14:05
  • @akhilsp i did, but i got some error, saying that "[06/Oct/2017:20:18:47 +0200] [pool-2-thread-7] http://10.12.214.20" is not an element of the list. Your answer is working fine! – sunjazz Oct 27 '17 at 14:15
  • Glad to know that! – akhilsp Oct 27 '17 at 14:17

2 Answers2

1

I think you are looking in the wrong list. Instead of looking in the index, try looking in content

with open('test.txt', "r") as file:
    for line in file:
        print find_element_in_list(content, line.rstrip('\n'))
akhilsp
  • 1,063
  • 2
  • 13
  • 26
0

To get the line number, you can try this:

file = open('filename.txt')
data = file.readlines()
target = "[06/Oct/2017:20:19:49 +0200] [pool-2-thread-8] http://10.12.214.18"
for i, item in enumerate(data, start=1):
    if item == target:
        print('Target is in line: ', str(i))
Elis Byberi
  • 1,422
  • 1
  • 11
  • 20
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • tried your solution, but its still printing out None. I do need the loop over the file later on, so i nested the "target" and "the_index" into the for loop like this: content = [i.strip('\n') for i in open(testfile)] with open(testfile) as f: for line in f: target = line index = [i for i,a in enumerate(content) if a == target] index = None if not index else index[0] print index – sunjazz Oct 27 '17 at 13:54
  • @sunjazz I approved the edit, however, the previous solution worked for me when I tested it. Did you use a different target string? – Ajax1234 Oct 27 '17 at 14:01
  • Change enumerate's argument "start" to 0 (zero). – Elis Byberi Oct 27 '17 at 14:03