自定义eachFile遍历文件夹文件

# 进入的path是个文件夹路径,里面的文件是txt
def eachFile(filepath):
    pathDir = os.listdir(filepath)  # 获取当前路径下的文件名,返回List
    title = []
    key1 = [];key2 = [];key3 = [];key4 = [];key5 = [];key6 = [];key7 = [];key8 = [];key9 = [];key10 = []
    key_words = [key1, key2, key3, key4, key5, key6, key7, key8, key9, key10]
    for s in pathDir:
        newDir = os.path.join(filepath, s)  # 将文件命加入到当前文件路径后面
        if os.path.isfile(newDir) and os.path.splitext(newDir)[1] == ".txt":  # 如果是txt文件
            with open(newDir, 'r') as f:
                content = f.read()
                if content:
                    title.append(s.replace('.txt', ''))
                    tags = jieba.analyse.extract_tags(content,
                            topK=10, allowPOS=('n','v'))
                    for i in range(len(key_words)):
                        try:
                            key_words[i].append(tags[i])
                        except:
                            key_words[i].append('') 

    result = pd.DataFrame({'title': title, 'key1': key1,
                           'key2': key2, 'key3': key3, 'key4': key4,
                           'key5': key5, 'key6': key6, 'key7': key7,
                           'key8': key8, 'key9': key9, 'key10': key10, },
                          columns=['title', 'key1', 'key2', 'key3', 'key4', 'key5',
                                   'key6', 'key7', 'key8', 'key9', 'key10', ])
    
    return result

  

转载于:https://www.cnblogs.com/ming666/p/9712170.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值