0

if I have file with words in order in which they were saved, new line comes from below

word 1 
word 2 
word 3
word 4 
word 5 

now if I have some word, for example "word 3", if "word 3" exist in text file, show me previous or next word from "word 3" in list order.

data = open('D:\path\file.txt', "r")
searchlines = data.readlines()
for q_numb, line in enumerate(searchlines):
    if ('word 3') in line:
        for l in searchlines:
            prev_line = l[:+1]
            print ('"word 3" found:',line, 'previous of "word 3" is:',prev_line)
            break
data.close()

my result, but wrong, previous must be "word 2"

"word 3" found: word 3
 previous of "word 3" is: word 1

and how go with plus, if I want next word after current?

my result with plus:

"word 3" found: word 3
 previous of "word 3" is: w

what I'm doing wrong?

2 Answers2

0

There are some problems in your code, this should work:

data = open('file.txt', "r")
searchlines = data.readlines()
for q_numb, line in enumerate(searchlines):
    if ('word 3') in line:
        prev_line = searchlines[q_numb - 1]
        print ('"word 3" found:',line, 'previous of "word 3" is:',prev_line)
        break
data.close()

What changed and why?

I think you do not understand the purpose of enumerate. You can read about it here: https://docs.python.org/3.5/library/functions.html#enumerate. So there is no reason to iterate twice over the list. The enumerate also provide (in your example) the current line number. To get the previous line you can simply use searchlines[q_numb - 1]. The colon is used to slice an array you can read about it here: Explain Python's slice notation

Community
  • 1
  • 1
Querenker
  • 2,242
  • 1
  • 18
  • 29
  • got it. I have one problem that I can not fix, maybe you can help me with this one too. I have received solution, it works well for part of goal and must, but does not performs the other for me, maybe because I'm doing something wrong, or because I'm on windows here is http://stackoverflow.com/questions/36511550/sort-words-in-sequence-order-in-which-they-were-saved –  Apr 09 '16 at 22:09
  • @L_Oni That is not the way Stackoverflow works in general, but I tried to help you also with this question :) – Querenker Apr 09 '16 at 22:41
  • yes yes I know, when I post it first time it was a one question, but then I have divided it into two separate, so I appreciate your support –  Apr 09 '16 at 22:58
0

Here is a technique if you want to print the previous word as well as the next one, if any:

prev_word, next_word, do_print = None, None, False
with open('file.txt','r') as f:
    for word in f:
        word = word.strip()
        if do_print:
            next_word = word
            break
        elif word == 'word 3':
            do_print = True
        else:
            prev_word = word

if do_print:
    print(prev_word, 'word3', next_word)

Multiple advantages using this technique:

  1. the file is read line by line, no big memory usage
  2. if there is another line after, the loop will do 1 more iteration to capture the next word
  3. the 2 OR 3 words will be printed if 'word3' got found
DevLounge
  • 8,313
  • 3
  • 31
  • 44