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 + '\n' for s in getssr())

 提取结果:

 

posted on 2018-10-30 09:20  一個未来  阅读(6787)  评论(0编辑  收藏  举报

导航