打印文本文件python中搜索键的第一次和最后一次出现之间的行

时间:2017-05-25 08:14:47

标签: python

我有一个文本文件,我想提取介于第一次和最后一次出现的短语包含" crypt"之间的行。我已经构建了以下代码,但它给出了包含" crypt"仅

fr = open('D:\\mydata\\dataset\\ds\\rw\\f1.txt')
fw = open('D:\\mydata\\dataset\\ds\\at\\f2.txt', 'w')
line = fr.readline()
while line:
        ##print (line)
    line = fr.readline()
    ##fw.writelines(line)
    if 'crypt' in line.lower():
        ##fw.close()
        ##break
        fw.writelines(line)
    ##fw.close()
fr.close()

有没有人知道如何在python中做到这一点。

以下是文本文件:

SetErrorMode  '32769'
LoadStringA  '0x73420000', '2001', 
GetSystemInfo  '1'
LoadResource  '0x004292f4', '0x004290c8', '0x00400000'
LdrGetProcedureAddress  '0', '0x7716b6e2', 'VarR4FromDec', 'OLEAUT32', '0x77120000'
ReadProcessMemory  '0x00001368', '0x00271ec0'
ReadProcessMemory  '0x0000dad8', '0x7ffd400c'
ReadProcessMemory  '0x0000dad8', '0x00191ea4'
ReadProcessMemory  '0x0000dad8', '0x00191ec0'
ReadProcessMemory  '0x0000dad8', '0x00020598'
Process32NextW  '0x0000dab4', 'spoolsvexe', , '0x7ffd9008'
ReadProcessMemory  '0x0000dae4', '0x7ffd900c'
ReadProcessMemory  '0x0000dae4', '0x00191ea4'
ReadProcessMemory  '0x0000dae4', '0x00191ec0'
ReadProcessMemory  '0x0000dae4', '0x00020598'
Process32NextW  '0x0000dab4', 'algexe', '1588'
NtOpenProcess  '0x00000410', '1588', '0x0000dae8'
ReadProcessMemory  '0x0000dae8', '0x7ffde008'
ReadProcessMemory  '0x0000dae8', '0x7ffde00c'
ReadProcessMemory  '0x0000dae8', '0x00191ea4'
NtClose  '0x00001304'
ReadProcessMemory  '0x0000dae8', '0x00191ec0'
ReadProcessMemory  '0x0000dae8', '0x00020598'
Process32NextW  '0x0000dab4', 'wpabalnexe', '1816'
NtOpenProcess  '0x00000410', '1816', '0x0000daec'
ReadProcessMemory  '0x0000daec', '0x7ffd8008'
ReadProcessMemory  '0x0000daec', '0x7ffd800c'
ReadProcessMemory  '0x0000daec', '0x00191ea4'
ReadProcessMemory  '0x0000daec', '0x00191ec0'
ReadProcessMemory  '0x0000daec', '0x00020598'
Process32NextW  '0x0000dab4', 'cryptainerleexe', '3392'
CreateToolhelp32Snapshot  '15', '0'
NtOpenProcess  '0x00000410', '3392', '0x0000daf0'
ReadProcessMemory  '0x0000daf0', '0x7ffd6008'
ReadProcessMemory  '0x0000daf0', '2132', '0x0000db10'
ReadProcessMemory  '0x0000db10', , '0x0000db4c'
ReadProcessMemory  '0x0000db4c', '0x7ffdd008'
ReadProcessMemory  '0x0000db4c', '0x7ffdd00c'
ReadProcessMemory  '0x0000db4c', '0x00191ea4'
ReadProcessMemory  '0x0000db4c', '0x00191ec0'
ReadProcessMemory  '0x0000db4c', '0x00020584'
Process32NextW  '0x0000db20', 'wscntfyexe', '1380'
ReadProcessMemory  '0x00001374', '0x00261ea4'
NtOpenProcess  '0x00000410', '1380', '0x0000db50'
ReadProcessMemory  '0x0000db50', '0x7ffd9008'
ReadProcessMemory  '0x0000db50', '0x7ffd900c'
ReadProcessMemory  '0x0000db50', '0x00191ea4'
ReadProcessMemory  '0x0000db50', '0x00191ec0'
ReadProcessMemory  '0x0000db50', '0x00020598'
Process32NextW  '0x0000db20', 'algexe', '1588'
NtOpenProcess  '0x00000410', '1588', '0x00020598'
Process32NextW  '0x0000db20', 'wpabalnexe', '1816'
NtOpenProcess  '0x00000410', '1816', '0x0000db58'
ReadProcessMemory  '0x0000db58', '0x7ffd8008'
ReadProcessMemory  '0x0000db58', '0x7ffd800c'
ReadProcessMemory  '0x0000db58', '0x00191ea4'
ReadProcessMemory  '0x0000db58', '0x00191ec0'
ReadProcessMemory  '0x0000db58', '0x00020598'
ReadProcessMemory  '0x00001374', '0x00110534'
Process32NextW  '0x0000db20', 'cryptainerleexe', '3392'
NtOpenProcess  '0x00000410', '3392', '0x0000db5c'
ReadProcessMemory  '0x0000db5c', '0x7ffd6008'
ReadProcessMemory  '0x0000db5c', '0x7ffd600c'
ReadProcessMemory  '0x0000db5c', '0x00251ea4'
ReadProcessMemory  '0x0000db5c', '0x00251ec0'
ReadProcessMemory  '0x0000db5c', '0x000205b4'
Process32NextW  '0x0000db20', 'chromeexe', '3596'
NtOpenProcess  '0x00000410', '3596', '0x0000db60'
ReadProcessMemory  '0x0000db60', '0x7ffdd008'
Process32NextW  '0x00001370', 'winlogonexe', '600'
ReadProcessMemory  '0x0000db60', '0x7ffdd00c'
ReadProcessMemory  '0x0000db60', '0x00251ea4'
ReadProcessMemory  '0x0000db60', '0x00251ec0'
ReadProcessMemory  '0x0000db60', '0x0002061c'
Process32NextW  '0x0000db20', 'chromeexe', '3468'
NtOpenProcess  '0x00000410', '3468', '0x0000db64'
ReadProcessMemory  '0x0000db64', '0x7ffdc008'
ReadProcessMemory  '0x0000db64', '0x7ffdc00c'
ReadProcessMemory  '0x0000db64', '0x00251ea4'
ReadProcessMemory  '0x0000db64', '0x00251ec0'
NtOpenProcess  '0x00000410', '600', '0x00001378'
ReadProcessMemory  '0x0000db64', '0x0002061c'
Process32NextW  '0x0000db20', 'chromeexe', '2260'
NtOpenProcess  '0x00000410', '2260', '0x0000db68'
ReadProcessMemory  '0x0000db68', '0x7ffd8008'
ReadProcessMemory  '0x0000db68', '0x7ffd800c'
ReadProcessMemory  '0x0000db68', '0x00841ea4'
ReadProcessMemory  '0x0000db68', '0x00841ec0'
ReadProcessMemory  '0x0000db68', '0x0002061c'
Process32NextW  '0x0000db20', 'chromeexe', '2664'
NtOpenProcess  '0x00000410', '2664', '0x0000db6c'
ReadProcessMemory  '0x00001378', '0x7ffdf008'
ReadProcessMemory  '0x0000db6c', '0x7ffdb008'
ReadProcessMemory  '0x0000db6c', '0x7ffdb00c'
ReadProcessMemory  '0x0000db6c', '0x00ea1ea4'
ReadProcessMemory  '0x0000db6c', '0x00ea1ec0'
ReadProcessMemory  '0x0000db6c', '0x0002061c'
Process32NextW  '0x0000db20', 'chromeexe', '2820'
NtOpenProcess  '0x00000410', '2820', '0x0000db70'
ReadProcessMemory  '0x0000db70', '0x7ffde008'
ReadProcessMemory  '0x0000db70', '0x7ffde00c'
ReadProcessMemory  '0x0000db70', '0x00271ea4'
ReadProcessMemory  '0x00001378', '0x7ffdf00c'
ReadProcessMemory  '0x0000db70', '0x00271ec0'
ReadProcessMemory  '0x0000db70', '0x0002061c'
Process32NextW  '0x0000db20', 'locatorexe', '332'
NtOpenProcess  '0x00000410', '332', '0x0000db74'
ReadProcessMemory  '0x0000db74', '0x7ffdd008'
ReadProcessMemory  '0x0000db74', '0x7ffdd00c'
ReadProcessMemory  '0x0000db74', '0x00191ea4'
ReadProcessMemory  '0x0000db74', '0x00191ec0'
ReadProcessMemory  '0x0000db74', '0x00020598'
Process32NextW  '0x0000db20', 'pythonexe', '1460'
ReadProcessMemory  '0x00001378', '0x00171ea4'
NtOpenProcess  '0x00000410', '1460', '0x0000db78'
ReadProcessMemory  '0x0000db78', '0x7ffdf008'
ReadProcessMemory  '0x0000db78', '0x7ffdf00c'
ReadProcessMemory  '0x0000db78', '0x00341ea4'
ReadProcessMemory  '0x0000db78', '0x00341ec0'
ReadProcessMemory  '0x0000db78', '0x00020588'
Process32NextW  '0x0000db20', 'pythonexe', '2132'
NtOpenProcess  '0x00000410', '2132', '0x0000db7c'
ReadProcessMemory  '0x0000db7c', '0x7ffdd008'
ReadProcessMemory  '0x0000db7c', '0x7ffdd00c'
ReadProcessMemory  '0x00001378', '0x00171ec0'
ReadProcessMemory  '0x0000db7c', '0x00341ea4'
ReadProcessMemory  '0x0000db7c', '0x00341ec0'
ReadProcessMemory  '0x0000db7c', '0x00020588'
Process32NextW  '0x0000db20', 'explorerexe', '2396'
NtOpenProcess  '0x00000410', '2396', '0x0000db80'
ReadProcessMemory  '0x0000db80', '0x7ffdd008'
ReadProcessMemory  '0x0000db80', '0x7ffdd00c'
ReadProcessMemory  '0x0000db80', '0x00271ea4'
ReadProcessMemory  '0x0000db80', '0x00271ec0'
ReadProcessMemory  '0x0000db80', '0x00020594'
LdrGetProcedureAddress  '0', '0x771699f0', 'SafeArrayGetRecordInfo', 'OLEAUT32', '0x77120000'
ReadProcessMemory  '0x00001378', '0x00020534'
Process32NextW  '0x0000db20', 'svchostexe', '3048'
NtOpenProcess  '0x00000410', '3048', '0x0000db84'
ReadProcessMemory  '0x0000db84', '0x7ffde008'
ReadProcessMemory  '0x0000db84', '0x7ffde00c'
ReadProcessMemory  '0x0000db84', '0x00271ea4'
ReadProcessMemory  '0x0000db84', '0x00271ec0'
ReadProcessMemory  '0x0000db84', '0x000205a8'
Process32NextW  '0x0000db20', '\uf3f4\x12', '0'
NtClose  '0x0000db20'
CreateToolhelp32Snapshot  '15', '0'
Process32NextW  '0x00001370', 'servicesexe', '644'
Process32FirstW  '0x0000db8c', '[SystemProcess]', '0'
NtOpenProcess  '0x00000410', '0', '0x00000000'
Process32NextW  '0x0000db8c', 'System', '4'
NtOpenProcess  '0x00000410', '4', '0x0000db20'
ReadProcessMemory  '0x0000db20', '0x00000008'
Process32NextW  '0x0000db8c', 'smssexe', '512'
NtOpenProcess  '0x00000410', '512', '0x0000db88'
ReadProcessMemory  '0x0000db88', '0x7ffdc008'
ReadProcessMemory  '0x0000db88', '0x7ffdc00c'
ReadProcessMemory  '0x0000db88', '0x00261ea4'
NtOpenProcess  '0x00000410', '644', '0x0000137c'
ReadProcessMemory  '0x0000db88', '0x00261ec0'
ReadProcessMemory  '0x0000db88', '0x001106a0'
Process32NextW  '0x0000db8c', 'csrssexe', '576'
NtOpenProcess  '0x00000410', '576', '0x0000db90'
ReadProcessMemory  '0x0000db90', '0x7ffdd008'
ReadProcessMemory  '0x0000db90', '0x7ffdd00c'
ReadProcessMemory  '0x0000db90', '0x00261ea4'
ReadProcessMemory  '0x0000db90', '0x00261ec0'
ReadProcessMemory  '0x0000db90', '0x00110534'
Process32NextW  '0x0000db8c', 'winlogonexe', '600'
ReadProcessMemory  '0x0000137c', '0x7ffdd008'
NtOpenProcess  '0x00000410', '600', '0x0000db94'
ReadProcessMemory  '0x0000db94', '0x7ffdf008'
ReadProcessMemory  '0x0000db94', '0x7ffdf00c'
ReadProcessMemory  '0x0000db94', '0x00171ea4'
ReadProcessMemory  '0x0000db94', '0x00171ec0'
ReadProcessMemory  '0x0000db94', '0x00020534'
Process32NextW  '0x0000db8c', 'servicesexe', '644'
NtOpenProcess  '0x00000410', '644', '0x0000db98'
ReadProcessMemory  '0x0000db98', '0x7ffdd008'
ReadProcessMemory  '0x0000db98', '0x7ffdd00c'
ReadProcessMemory  '0x0000137c', '0x7ffdd00c'
ReadProcessMemory  '0x0000db98', '0x00191ea4'
ReadProcessMemory  '0x0000db98', '0x00191ec0'
ReadProcessMemory  '0x0000db98', '0x00020598'
Process32NextW  '0x0000db8c', 'lsassexe', '656'
NtOpenProcess  '0x00000410', '656', '0x0000db9c'
ReadProcessMemory  '0x0000db9c', '0x7ffd9008'
ReadProcessMemory  '0x0000db9c', '0x7ffd900c'
ReadProcessMemory  '0x0000db9c', '0x00191ea4'
ReadProcessMemory  '0x0000db9c', '0x00191ec0'
ReadProcessMemory  '0x0000db9c', '0x00020598'
ReadProcessMemory  '0x0000137c', '0x00191ea4'
Process32NextW  '0x0000db8c', 'svchostexe', '824'
NtOpenProcess  '0x00000410', '824', '0x0000dba0'
ReadProcessMemory  '0x0000dba0', '0x7ffd7008'
ReadProcessMemory  '0x0000dba0', '0x7ffd700c'
ReadProcessMemory  '0x0000dba0', '0x00191ea4'
ReadProcessMemory  '0x0000dba0', '0x00191ec0'
ReadProcessMemory  '0x0000dba0', '0x00020598'
Process32NextW  '0x0000db8c', 'svchostexe', '896'
NtOpenProcess  '0x00000410', '896', '0x0000dba4'
ReadProcessMemory  '0x0000dba4', '0x7ffde008'
ReadProcessMemory  '0x0000137c', '0x00191ec0'
ReadProcessMemory  '0x0000dba4', '0x7ffde00c'
ReadProcessMemory  '0x0000dba4', '0x00191ea4'
ReadProcessMemory  '0x0000dba4', '0x00191ec0'
ReadProcessMemory  '0x0000dba4', '0x00020598'
Process32NextW  '0x0000db8c', 'svchostexe', '1012'
NtOpenProcess  '0x00000410', '1012', '0x0000dba8'
ReadProcessMemory  '0x0000dba8', '0x7ffdb008'
ReadProcessMemory  '0x0000dba8', '0x7ffdb00c'
ReadProcessMemory  '0x0000dba8', '0x00191ea4'
ReadProcessMemory  '0x0000dba8', '0x00191ec0'
ReadProcessMemory  '0x0000137c', '0x00020598'
ReadProcessMemory  '0x0000dba8', '0x00020598'
Process32NextW  '0x0000db8c', 'svchostexe', '1084'
NtOpenProcess  '0x00000410', '1084', '0x0000dbac'
ReadProcessMemory  '0x0000dbac', '0x7ffde008'
ReadProcessMemory  '0x0000dbac', '0x7ffde00c'
ReadProcessMemory  '0x0000dbac', '0x00191ea4'
ReadProcessMemory  '0x0000dbac', '0x00191ec0'
ReadProcessMemory  '0x0000dbac', '0x00020598'
Process32NextW  '0x0000db8c', 'svchostexe', '1164'
NtOpenProcess  '0x00000410', '1164', '0x0000dbb0'
Process32NextW  '0x00001370', 'lsassexe', '656'
ReadProcessMemory  '0x0000dbb0', '0x7ffd4008'
ReadProcessMemory  '0x0000dbb0', '0x7ffd400c'
ReadProcessMemory  '0x0000dbb0', '0x00191ea4'
ReadProcessMemory  '0x0000dbb0', '0x00191ec0'
ReadProcessMemory  '0x0000dbb0', '0x00020598'
Process32NextW  '0x0000db8c', 'spoolsvexe', '1436'
NtOpenProcess  '0x00000410', '1436', '0x0000dbb4'
ReadProcessMemory  '0x0000dbb4', '0x7ffda008'
ReadProcessMemory  '0x0000dbb4', '0x7ffda00c'
ReadProcessMemory  '0x0000dbb4', '0x00191ea4'
NtOpenProcess  '0x00000410', '656', '0x00001380'
ReadProcessMemory  '0x0000dbb4', '0x00191ec0'
ReadProcessMemory  '0x0000dbb4', '0x00020598'
Process32NextW  '0x0000db8c', 'explorerexe', '1560'
NtOpenProcess  '0x00000410', '1560', '0x0000dbb8'
ReadProcessMemory  '0x0000dbb8', '0x7ffdd008'
ReadProcessMemory  '0x0000dbb8', '0x7ffdd00c'
ReadProcessMemory  '0x0000dbb8', '0x00191ea4'
ReadProcessMemory  '0x0000dbb8', '0x00191ec0'
ReadProcessMemory  '0x0000dbb8', '0x00020584'
Process32NextW  '0x0000db8c', 'wscntfyexe', '1380'
LdrGetProcedureAddress  '0', '0x77169a2a', 'SafeArraySetRecordInfo', 'OLEAUT32', '0x77120000'
ReadProcessMemory  '0x00001380', '0x7ffd9008'
NtOpenProcess  '0x00000410', '1380', '0x0000dbbc'
ReadProcessMemory  '0x0000dbbc', '0x7ffd9008'
ReadProcessMemory  '0x0000dbbc', '0x7ffd900c'
ReadProcessMemory  '0x0000dbbc', '0x00191ea4'
ReadProcessMemory  '0x0000dbbc', '0x00191ec0'
ReadProcessMemory  '0x0000dbbc', '0x00020598'
Process32NextW  '0x0000db8c', 'algexe', '1588'
NtOpenProcess  '0x00000410', '1588', '0x0000dbc0'
ReadProcessMemory  '0x0000dbc0', '0x7ffde008'
ReadProcessMemory  '0x0000dbc0', '0x7ffde00c'
ReadProcessMemory  '0x00001380', '0x7ffd900c'
ReadProcessMemory  '0x0000dbc0', '0x00191ea4'
ReadProcessMemory  '0x0000dbc0', '0x00191ec0'
ReadProcessMemory  '0x0000dbc0', '0x00020598'
Process32NextW  '0x0000db8c', 'wpabalnexe', '1816'
NtOpenProcess  '0x00000410', '1816', '0x0000dbc4'
ReadProcessMemory  '0x0000dbc4', '0x7ffd8008'
ReadProcessMemory  '0x0000dbc4', '0x7ffd800c'
ReadProcessMemory  '0x0000dbc4', '0x00191ea4'
ReadProcessMemory  '0x0000dbc4', '0x00191ec0'
ReadProcessMemory  '0x0000dbc4', '0x00020598'
ReadProcessMemory  '0x00001380', '0x00191ea4'
Process32NextW  '0x0000db8c', 'cryptainerleexe', '3392'
NtOpenProcess  '0x00000410', '3392', '0x0000dbc8'
ReadProcessMemory  '0x0000dbc8', '0x7ffd6008'
ReadProcessMemory  '0x0000dbc8', '0x7ffd600c'
ReadProcessMemory  '0x0000dbc8', '0x00251ea4'
ReadProcessMemory  '0x0000dbc8', '0x00251ec0'
ReadProcessMemory  '0x0000dbc8', '0x000205b4'
Process32NextW  '0x0000db8c', 'chromeexe', '3596'
NtOpenProcess  '0x00000410', '3596', '0x0000dbcc'
ReadProcessMemory  '0x0000dbcc', '0x7ffdd008'
ReadProcessMemory  '0x00001380', '0x00191ec0'
ReadProcessMemory  '0x0000dbcc', '0x7ffdd00c'
ReadProcessMemory  '0x0000dbcc', '0x00251ea4'
ReadProcessMemory  '0x0000dbcc', '0x00251ec0'
ReadProcessMemory  '0x0000dbcc', '0x0002061c'
Process32NextW  '0x0000db8c', 'chromeexe', '3468'
NtOpenProcess  '0x00000410', '3468', '0x0000dbd0'
ReadProcessMemory  '0x0000dbd0', '0x7ffdc008'
ReadProcessMemory  '0x0000dbd0', '0x7ffdc00c'
ReadProcessMemory  '0x0000dbd0', '0x00251ea4'
ReadProcessMemory  '0x0000dbd0', '0x00251ec0'
ReadProcessMemory  '0x00001380', '0x00020598'
ReadProcessMemory  '0x0000dbd0', '0x0002061c'
Process32NextW  '0x0000db8c', 'chromeexe', '2260'
NtOpenProcess  '0x00000410', '2260', '0x0000dbd4'
ReadProcessMemory  '0x0000dbd4', '0x7ffd8008'
ReadProcessMemory  '0x0000dbd4', '0x7ffd800c'
ReadProcessMemory  '0x0000dbd4', '0x00841ea4'
ReadProcessMemory  '0x0000dbd4', '0x00841ec0'
ReadProcessMemory  '0x0000dbd4', '0x0002061c'
Process32NextW  '0x0000db8c', 'chromeexe', '2664'
NtOpenProcess  '0x00000410', '2664', '0x0000dbd8'

1 个答案:

答案 0 :(得分:1)

您可以通过这两行使用for loopsiterate并检查隐藏状态:

with open('file.txt', 'r') as f:
    with open('file2.txt', 'w') as w:
        data = f.readlines()
        for i in xrange(len(data)):
            if 'crypt' in data[i]:
                for j in xrange(i+1, len(data)):
                    if 'crypt' in data[j]:
                        break
                    w.write(data[j])

如果你想要在第一次和最后一次出现之间包含所有事件,以便在其中删除两次出现的事件,你可以loop通过列表并保存第一次出现index然后loop通过reverse { {1}}项和list检查最后一次出现并保存loop,然后第三次index来写入数据:

loop