你能格式化一个列举的列表吗



我正试图在枚举列表中使用.replace和.lstrip。有没有一个变通办法,因为它似乎对元组不起作用?

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

代码和输出示例

我希望能够删除换行符。当我添加到print(句子(行时,它告诉我元组没有对象替换。

enumerate(iterable,start(函数将返回元组序列。如果我们像这样正常地循环这个序列,我们在每次迭代中都会得到一个元组:
for t in enumerate(iterable):
print(t) #t is a tuple

但是我们上面展示的语法在每次迭代中都会对元组进行解包,所以:

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)

最新更新