I am trying to get every element from python list returned as a string, but it returns only the first element of the list, not continuing the loop.
Main Code (prishot.py)
JavaScript
x
21
21
1
import trafilatura
2
3
class prishot:
4
def __init__(self, url):
5
self.url = url
6
7
8
def ps(self):
9
downloaded = trafilatura.fetch_url(self.url)
10
trafilatura.extract(downloaded)
11
12
a = trafilatura.extract(downloaded, include_links=False, include_comments=False, include_tables=False, no_fallback=True)
13
s = [sentence + '.' for sentence in a.split('.') if 'data' in sentence]
14
index_list = [index for index, sentence in enumerate(s)]
15
16
list_length = len(index_list) - 1
17
num_z = 0
18
while num_z < list_length:
19
return s[num_z]
20
num += 1
21
Test code to run the above (test.py)
JavaScript
1
6
1
from prishot import prishot
2
3
a = prishot('https://www.intuit.com/privacy/statement/')
4
5
print(a.ps())
6
After running the test.py it gives me only the first sentence in the list s in prishot.py: Screenshot of CMD
But if I try printing the index_list (which is in prishot.py) without the rest, you can clearly see, that there are 21 indexes there. Screenshot of CMD
So here is the output, I want it to be. As you can see here are all the sentences, which are stored in list s in prishot.py. When running test.py it returns only the first sentence. I need to return the rest just the same as in the first picture. All the sentences First sentence output
Advertisement
Answer
You can use yield for creating a generator
JavaScript
1
6
1
list_length = len(index_list) - 1
2
num_z = 0
3
while num_z < list_length:
4
yield s[num_z]
5
num += 1
6
By adding this on test.py
JavaScript
1
2
1
print(*a.ps(),sep='n')
2
OR,
You can try
As this outputs line by line with no change in test.py
.
JavaScript
1
4
1
list_length = len(index_list) - 1
2
num_z = 0
3
return 'n'.join(s)
4