python textrank 将摘要分别存入csv

请问如何将每一个摘要分别存为csv的一行?

以下是已经有的代码。整个过程是在用textrank提取多个文献的摘要

import sys
from imp import reload
import os
 
try:
    reload(sys)
    sys.setdefaultencoding('utf-8')
except:
    pass
 
import codecs
from textrank4zh import TextRank4Keyword, TextRank4Sentence
 
def work(file):
    # 打开并读取文本文件
 
    text = codecs.open(file, 'r', 'utf-8').read()
 
    # 创建分词类的实例
 
    tr4w = TextRank4Keyword()
 
    # 对文本进行分析,设定窗口大小为2,并将英文单词小写
 
    tr4w.analyze(text=text, lower=True, window=2)
 
    """输出"""
 
    print('关键词为:')
 
    # 从关键词列表中获取前20个关键词
 
    for item in tr4w.get_keywords(num=20, word_min_len=1):
        print(item.word, item.weight)
        print('\n')
        print('关键短语为:')
    # 从关键短语列表中获取关键短语
 
    for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num=2):
        print(phrase)
        print('\n')
    # 创建分句类的实例
 
    tr4s = TextRank4Sentence()
 
    # 英文单词小写,进行词性过滤并剔除停用词
 
    tr4s.analyze(text=text, lower=True, source='no_filter')
 
    print('摘要为:')
 
    # 抽取3条句子作为摘要
 
    for item in tr4s.get_key_sentences(num=3):
        # 打印句子的索引、权重和内容
        print(item.index, item.weight, item.sentence)
 
def check_all_files(check_path):
    list_files = []
    # 列出文件夹下所有文件
    cur_list = os.listdir(check_path)
    for i in range(0 ,len(cur_list)):
        file_path = os.path.join(check_path, cur_list[i])
        if os.path.isfile(file_path):
            if cur_list[i].upper()[-4:]=='.TXT':
                list_files.append([cur_list[i], file_path])
    return list_files
 
 
f_lst = check_all_files(r"/Users/xiongying/Desktop")
 
for f in f_lst:
    print("处理",f[0])
    work(f[1])

这样试试, 我代码里写了注释

import sys
from imp import reload
import os

try:
    reload(sys)
    sys.setdefaultencoding('utf-8')
except:
    pass

import codecs
from textrank4zh import TextRank4Keyword, TextRank4Sentence

def work(file):
    # file = r'/Users/xiongying/Desktop/有关农业经济的几个问题_许涤新.txt'

    # 打开并读取文本文件

    text = codecs.open(file, 'r', 'utf-8').read()

    # 创建分词类的实例

    tr4w = TextRank4Keyword()

    # 对文本进行分析,设定窗口大小为2,并将英文单词小写

    tr4w.analyze(text=text, lower=True, window=2)

    """输出"""

    print('关键词为:')

    # 从关键词列表中获取前20个关键词

    for item in tr4w.get_keywords(num=20, word_min_len=1):
        print(item.word, item.weight)
        print('\n')
        print('关键短语为:')
    # 从关键短语列表中获取关键短语

    for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num=2):
        print(phrase)
        print('\n')
    # 创建分句类的实例

    tr4s = TextRank4Sentence()

    # 英文单词小写,进行词性过滤并剔除停用词

    tr4s.analyze(text=text, lower=True, source='no_filter')

    print('摘要为:')

    # 抽取3条句子作为摘要
    zy = []

    for item in tr4s.get_key_sentences(num=3):
        # 打印句子的索引、权重和内容
        print(item.index, item.weight, item.sentence)
        zy.append(" , ".join([item.index, item.weight, item.sentence]))
    # 这里返回摘要信息
    return "\n".join(zy)

def check_all_files(check_path):
    list_files = []
    # 列出文件夹下所有文件
    cur_list = os.listdir(check_path)
    for i in range(0 ,len(cur_list)):
        file_path = os.path.join(check_path, cur_list[i])
        if os.path.isfile(file_path):
            if cur_list[i].upper()[-4:]=='.TXT':
                list_files.append([cur_list[i], file_path])
    return list_files


f_lst = check_all_files(r"/Users/xiongying/Desktop")
with open("you_zy.csv", 'a') as csv_file:
    for f in f_lst:
        print("处理",f[0])
        # 写入摘要文件
        csv_file.write("{}\n".format(work(f[1])))