can you format an enumerated list?

Question:

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('')

Example of code and output

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.

Asked By: nji1997

||

Answers:

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)
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.