vendredi 25 décembre 2020

Problem with printing elements of enumerate function in python

This is a part of my python application code which is located in a for loop itself. It finds every file's name from another text file. I have tested every part of this code. When i print i it prints the right line number every time. The problem is with the sen. When i print sen, it prints just a new line(\n). Why doesn't it contain every line's content?

line_number=1
for line in open(relevant_docs_path, "r",encoding="utf8"):

       if line.rstrip('\n') == file_name:

         with open(relevant_docs_path) as fp:
           for i, sen in enumerate(fp):
               if i == line_number+1:
                  print(sen)
         break
       else:
         line_number += 1 

and this is my text file(fp):

1.persian_query
7894.persian_poem 7895.persian_poem 7898.persian_poem 7899.persian_poem 7900.persian_poem

2.persian_query
7901.persian_poem 7902.persian_poem

3.persian_query
7901.persian_poem 7902.persian_poem

4.persian_query
7903.persian_poem

5.persian_query
7904.persian_poem 7905.persian_poem 7906.persian_poem 7907.persian_poem 7908.persian_poem 7909.persian_poem 7910.persian_poem 7911.persian_poem 7912.persian_poem 7913.persian_poem 7914.persian_poem 7915.persian_poem 7916.persian_poem 7917.persian_poem 7918.persian_poem 7919.persian_poem 7920.persian_poem 7921.persian_poem 7922.persian_poem 7923.persian_poem 7924.persian_poem

Aucun commentaire:

Enregistrer un commentaire