python提取批量文件内的指定内容

目标文件夹:

文件内容:


实现代码:

# -*- coding:utf-8 -*-
# __author__ :kusy
# __content__:get ssr info from html files
# __date__:2018/10/29 14:43

import os
import re


def getssr():
    srcdir = os.path.abspath('src')
    files = os.listdir(srcdir)
    filestr = ''
    for file in files:
        with open(srcdir + '\' + file,'r',encoding='utf-8') as f:
            filestr = filestr + f.read()

    regexp = re.compile('ssr://[A-Za-z0-9]*')
    mystr = regexp.findall(filestr)
    # 去重
    mystr = list(set(mystr))
    return mystr

if __name__ == '__main__':
    with open(os.path.abspath('ssr.log'),'w') as newfile:
        newfile.writelines(s + '
' for s in getssr())

 提取结果:

原文地址:https://www.cnblogs.com/kusy/p/9872293.html