python工具 - 讀取文件的部分指定內容並輸出到外置窗口


一、使用場景

  某些配置文件里有一些特定的字符,而這些字符恰巧需要我們采集出來,然后輸出到另外一個窗口做展示時,可以使用該工具。

本例的演示則提取配置文件中的【姓名:黃蓉 女 九陰真經、姓名:郭靖 男 彈指神功】兩行讀取出來,讀取其他內容也可以,更改相應參數即可。

二、實驗文件

  測試腳本:fielread.py

  配置文件:john.conf

三、測試腳本代碼

#! /usr/bin/env python3
# -*- coding:UTF-8 -*-

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中

# [1]控制台打印輸出
result = '\n'.join(info)
print(result)

# [2]alert彈窗打印輸出
root = Tk() # 創建窗口對象的背景顏色
new_info = Listbox(root)
for item in info:
    new_info.insert(0,item)
new_info.pack()
root.mainloop()

四、配置文件內容

#
# This file is part of John the Ripper password cracker,
# Copyright (c) 1996-2006,2008-2013 by Solar Designer
#
#

void init()
{
    password_length = 16; /* Change this to match config */

    int c, i;

    c = '0'; i = 0; while (c <= '9') numbers[i++] = c++;
    c = 'a'; i = 0; while (c <= 'z') lowers[i++] = c++;
    c = 'A'; i = 0; while (c <= 'Z') uppers[i++] = c++;

    /* symbols */
    i = 0;
    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++] = 536870912; boundaries_charclass[i++] = 1073741824;
    boundaries_charclass[i++] = 1610612736; boundaries_charclass[i++] = 2147483647;

    姓名:黃蓉 女 九陰真經
    姓名:郭靖 男 彈指神功

    備注:
    boundaries_numbers = 214748365
    boundaries_numbers = 644245095
    boundaries_numbers = 1073741824
    boundaries_numbers = 1503238554
    boundaries_numbers = 1932735284

    boundaries_letters[i++] = 82595525; boundaries_letters[i++] = 165191050;
    boundaries_letters[i++] = 247786575; boundaries_letters[i++] = 330382100;
    boundaries_letters[i++] = 412977625; boundaries_letters[i++] = 495573150;
}

# 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
姓名:黃蓉 女 九陰真經
姓名:郭靖 男 彈指神功

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM