-4

I want to save my result number on list, but i cant perform that. Why i cant do that? I Have perform append() for make "numbers" in my result_list, but it didnt work. I using a PyPDF2 for processing PDF Files.

object = PyPDF2.PdfFileReader(r"storytime.pdf")

# Get number of pages
NumPages = object.getNumPages()

# Get specific word that i want to search for page position
angry_text = "Why you do that?"
sad_text = "if only we"
happy_text = "Yeah, we will"

searched_text_list = [angry_text, sad_text, happy_text]

for text_search in searched_text_list:
    for every_page in range(0, NumPages):
        magic_search = object.getPage(every_page)
        Stories = magic_search.extractText()
        if re.search(text_search, Stories):      
           result_page_search = every_page+1
           result_list = []
           result_list.append(result_page_search)
           print(result_list)

My Output

[108]
[200]
[300]

Expected Output :

[108, 200, 300]
  • 3
    What did you intend to achieve by `result_list = []`? – mkrieger1 Jan 25 '22 at 22:42
  • You erase `result_list` in each loop iteration, by setting it to a new empty list, via `result_list = []`. You append one item to it, then print it, then throw away the list completely on the next loop iteration. What do you expect to happen? Did you maybe intend to create the list before you start looping? – Random Davis Jan 25 '22 at 22:42
  • Hmm... i intend to store all printed number to some list with name result_list.. so, how i can fix this? any suggestion? – Falco_z Jan 25 '22 at 22:44
  • 2
    If you want your list to hold more than 1 item - do not re-assign its reference to empty list before each append. – PM 77-1 Jan 25 '22 at 22:47
  • Did you repost this question? You're still making an empty list, appending one element, then printing that `for every_page in range(0, NumPages)` – OneCricketeer Jan 25 '22 at 23:05
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 03 '22 at 23:04

1 Answers1

0

Hint: If you want to print only one list, your print statement shouldn't be in a loop

For example

result_list = []
for every_page in range(0, NumPages):
    magic_search = object.getPage(every_page)
    Stories = magic_search.extractText()
    if re.search(text_search, Stories):      
       result_page_search = every_page+1  
       result_list.append(result_page_search)
print(result_list)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • No, my goal was i want to take all number that i have chosen before in searched_text_list [] to be collected in result[]. Honestly your code only grip one number, not three. – Falco_z Jan 26 '22 at 01:10
  • Well, I don't have access to your PDF files to test. You can move `result_list = []` immediately after/before your `searched_text_list`. The point is still the same - create one empty list, append to it multiple times without creating new empty lists, then print once outside of the loops – OneCricketeer Jan 26 '22 at 05:05