I am trying to use .replace and .lstrip on an enumerated list. is there a workaround since it doesnt seem to work on tuples?
for file in onlyfiles:#for file in current directory
with fitz.open(file) as doc: #open the file
text="" #create a blank text variable
for page in doc: # for each page in the file
text = page.get_text() # add the text from the page to the blank text block
text_1 = re.split('\.(?![0-9])', text)
#print(text_1)
words = ['increase','growth']
print(f'File name: {file}')
for word in words:
print(f'Keyword: {word.title()}')
print('')
sentences = [sentence for sentence in text_1 if word.lower() in sentence.lower()]
for sentence in enumerate(sentences, start=1):
print(sentence)
print('')
I would like to be able to remove newline characters. When i added to the print(sentence) line, it told me tuples have no object replace.
CodePudding user response:
The enumerate(iterable, start) function will return a sequence of tuples. If we loop through that sequence normally like this, we get a tuple on each iteration:
for t in enumerate(iterable):
print(t) #t is a tuple
But the syntax we showed above unpacks that tuple on each iteration, so:
for a, b in enumerate(iterable):
print(a) #first value in the tuple (the count)
print(b) #second value in the tuple (the item in the original iterable)