python工具 - 读取文件的部分指定内容并输出到外置窗口

一、使用场景

  某些配置文件里有一些特定的字符,而这些字符恰巧需要我们采集出来,然后输出到另外一个窗口做展示时,可以使用该工具。

本例的演示则提取配置文件中的【姓名:黄蓉 女 九阴真经、姓名:郭靖 男 弹指神功】两行读取出来,读取其他内容也可以,更改相应参数即可。

二、实验文件

  测试脚本:fielread.py

  配置文件:john.conf

三、测试脚本代码

#! /usr/bin/env python3
# -*- coding:UTF- -*- from tkinter import *
import tkinter ss = input('请输入搜索内容的开始首字符:')
es = input('请输入搜索内容的结束首字符:')
fp = input('请输入要搜索文件的路径:') def fetch(startStr,endStr,filePath):
newLi = []
with open(filePath,'r',encoding='utf-8') as f :
flag = False
for line in f:
if line.strip().startswith(startStr):
flag = True if line.strip().startswith(endStr):
flag = False if flag and line.strip():
newLi.append(line.strip())
return newLi info = fetch(ss,es,fp) # 调用fetch函数进行内容匹配,结果返回列表保存到info中 # []控制台打印输出
result = '\n'.join(info)
print(result) # []alert弹窗打印输出
root = Tk() # 创建窗口对象的背景颜色
new_info = Listbox(root)
for item in info:
new_info.insert(,item)
new_info.pack()
root.mainloop()

四、配置文件内容

#
# This file is part of John the Ripper password cracker,
# Copyright (c) -,- by Solar Designer
#
# void init()
{
password_length = ; /* Change this to match config */ int c, i; c = ''; i = ; while (c <= '') numbers[i++] = c++;
c = 'a'; i = ; while (c <= 'z') lowers[i++] = c++;
c = 'A'; i = ; while (c <= 'Z') uppers[i++] = c++; /* symbols */
i = ;
symbols[i++] = '!'; symbols[i++] = '@'; symbols[i++] = '#'; symbols[i++] = '$';
symbols[i++] = '%'; symbols[i++] = '^'; symbols[i++] = '&'; symbols[i++] = '*';
symbols[i++] = '('; symbols[i++] = ')'; symbols[i++] = '~'; symbols[i++] = '-'; boundaries_charclass[i++] = ; boundaries_charclass[i++] = ;
boundaries_charclass[i++] = ; boundaries_charclass[i++] = ; 姓名:黄蓉 女 九阴真经
姓名:郭靖 男 弹指神功 备注:
boundaries_numbers =
boundaries_numbers =
boundaries_numbers =
boundaries_numbers =
boundaries_numbers = boundaries_letters[i++] = ; boundaries_letters[i++] = ;
boundaries_letters[i++] = ; boundaries_letters[i++] = ;
boundaries_letters[i++] = ; boundaries_letters[i++] = ;
} # End of john.conf file.
# Keep this comment, and blank line above it, to make sure a john-local.conf
# that does not end with \n is properly loaded.

五、执行效果 

Console:

请输入搜索内容的开始首字符:姓名
请输入搜索内容的结束首字符:备注
请输入要搜索文件的路径:./john.conf
姓名:黄蓉 女 九阴真经
姓名:郭靖 男 弹指神功

python工具 - 读取文件的部分指定内容并输出到外置窗口

  

上一篇:SLAM学习笔记(1)基本概念


下一篇:CUBA 7:崭新的篇章