将文本摘要生成器的输出写入文件会替换之前的摘要

Writing output of a text summarizer to a file replaces the previous summary

我正在尝试将 pprint 的输出写入文件,但这样做会替换我之前的输出。

def summarize_snippets(seg):
  
  for  seg in truncated_sentence:
    input_text = ' '.join(seg) 
    summary_text = summarizer(input_text, min_length=50, max_length=80)
    summ = summary_text[0].get('summary_text')
    pp.pprint(summary_text[0].get('summary_text'))
    with open("/content/drive/MyDrive/path/summary.txt", "w+") as summ_file:
      pp.pprint((summary_text[0].get('summary_text')), summ_file)

我希望所有显示为输出的内容都在文件中,而不仅仅是最后一个。 我试图将写命令置于循环之外,但它给出了错误。有没有更好的方法来实现这个?

我试图在调用函数 summarize_snippets(seg) 时写入文件,它仍然覆盖了之前的输出。

with open("/content/drive/MyDrive/crystal_ball/output/summary/summary.txt", "w+") as summ_file:
  pp.pprint(summarize_snippets (seg), summ_file)

我通过如下简单的修复解决了我的问题 -

    def summarize_snippets(joined_sentence):
  summ_text= [] 
  for  seg in joined_sentence:
    #input_text = seg
    summary_text = summarizer(seg, min_length=50, max_length=80)
    summ = summary_text[0].get('summary_text')
    summ_text.append(summ)
    pp.pprint(summary_text[0].get('summary_text'))
  with open('/content/drive/MyDrive/path/summary.txt', 'w') as f:
    for item in summ_text:
        f.write("%s\n\n\n\n\n\n" % item)