一个从文件中过滤指定字符串的python3脚本

from tabulate import tabulate

plugin = [
...
]

plugin_v1 = [
...
]

filepath = "E:\\PycharmProjects\\code\\file\\plugin.log"

# 创建一个一定长度的列表,每个元素赋值为0,其index 与 plugin+plugin_v1 的index 对应
# 就是使用两个列表,一个存key,一个存value,它们之间通过index来关联
calcuacess = [0] * (len(plugin) + len(plugin_v1))

with open(filepath,'r') as file:
    while True:
        line = file.readline()
        if line == '':
            break
        if line.find("app/plugin") != -1 :
            for index,x in enumerate(plugin):
                if line.find(x) != -1:
                    calcuacess[index] += 1

        if line.find("app/v1.0/plugin") != -1:
            for index,x in enumerate(plugin_v1):
                if line.find(x) != -1:
                    calcuacess[index + 5] += 1
d = []
for index,x in enumerate(plugin + plugin_v1):
    if index < 5 :
        ver = "plugin"
    elif index >= 5 :
        ver = "plugin_v1"
    d.append([ver,x.strip("fileId="), calcuacess[index]])

print(tabulate(d, headers=["版本","文件名", "下载量"]))

print("总数: ",sum(calcuacess))
posted @ 2022-08-01 09:39  武平宁  阅读(110)  评论(0)    收藏  举报