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)
import trafilatura
class prishot:
def __init__(self, url):
self.url = url
def ps(self):
downloaded = trafilatura.fetch_url(self.url)
trafilatura.extract(downloaded)
a = trafilatura.extract(downloaded, include_links=False, include_comments=False, include_tables=False, no_fallback=True)
s = [sentence + '.' for sentence in a.split('.') if 'data' in sentence]
index_list = [index for index, sentence in enumerate(s)]
list_length = len(index_list) - 1
num_z = 0
while num_z < list_length:
return s[num_z]
num += 1
Test code to run the above (test.py)
from prishot import prishot
a = prishot('https://www.intuit.com/privacy/statement/')
print(a.ps())
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
list_length = len(index_list) - 1
num_z = 0
while num_z < list_length:
yield s[num_z]
num += 1
By adding this on test.py
print(*a.ps(),sep='n')
OR,
You can try
As this outputs line by line with no change in test.py.
list_length = len(index_list) - 1 num_z = 0 return 'n'.join(s)