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
CodePudding user response:
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)
CodePudding user response:
It seems you want the whole list to be printed. if so then follow this 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]
for sentence in s:
print(sentence)
test.py:
from prishot import prishot
a = prishot('https://www.intuit.com/privacy/statement/')
a.ps() # To print all sentences
CodePudding user response:
In the while
loop you directly call return
and that's what happens - the function directly returns a value. What you probably want to do instead, is collect the values in a list (via my_list.append(...)
) and return after the while loop
(not in it).
CodePudding user response:
Now, it seems that you want to print the elements of the list and return the list from the function ps(). To do this, you could replace
num_z = 0
while num_z < list_length:
return s[num_z]
num = 1
with
print(*s, sep='\n')
return s
With your recent comment, it seems that you actually want each element of the list printed on a separate line. If that is the case, replace
num_z = 0
while num_z < list_length:
return s[num_z]
num = 1
with
return s
and replace
print(a.ps())
with
for line in a.ps():
print(line)
It looks like you simply want the whole list of strings concatenated together into a single string. If that is the case, replace
num_z = 0
while num_z < list_length:
return s[num_z]
num = 1
with
return ''.join(s)