如何根据内容分组文本文件?(How to group text files based on content?)

编程入门 行业动态 更新时间:2024-10-28 20:22:58
如何根据内容分组文本文件?(How to group text files based on content?)

我有2个文本文件,其中包含以下内容:

文字文件1: -

Imports KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA Exports

文字文件2: -

Imports KERNEL32.DLL 0x419128 LoadLibraryA 0x41912c GetProcAddress 0x419130 ExitProcess advapi32.dll 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString Exports

我想要一个包含公共输出的文本文件,以便根据.DLL名称对它们进行分组。 例如; 这是我想要的输出文件:

KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString

我编写了一个脚本,它将读取文件名并继续将输出附加到textfile final.txt 。 我可以使用Python的哪些功能根据标题对值进行分组?

#start import sys value = sys.argv[1] print value # Value is the filename with open(value) as inputd: # Parse file for line in inputd: if line.strip() == 'Imports': break for line in inputd: if line.strip() == 'Exports': break if "none" not in line: print line.rstrip() # print the line with open("final.txt", "a") as outputd: outputd.write(line) # write output to file #end

我目前的输出如下:

KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA KERNEL32.DLL 0x419128 LoadLibraryA 0x41912c GetProcAddress 0x419130 ExitProcess advapi32.dll 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString

I have 2 text files with the following content:

Text file 1:-

Imports KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA Exports

Text file2:-

Imports KERNEL32.DLL 0x419128 LoadLibraryA 0x41912c GetProcAddress 0x419130 ExitProcess advapi32.dll 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString Exports

I want a text file containing common outputs such that they are grouped based on the .DLL names. For e.g; This is my desired output file:

KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString

I have written a script which will read the filename and keep appending the output to a textfile final.txt. What function of Python can I use to group the values based on the headings?

#start import sys value = sys.argv[1] print value # Value is the filename with open(value) as inputd: # Parse file for line in inputd: if line.strip() == 'Imports': break for line in inputd: if line.strip() == 'Exports': break if "none" not in line: print line.rstrip() # print the line with open("final.txt", "a") as outputd: outputd.write(line) # write output to file #end

My current output is as follows:

KERNEL32.DLL 0x40d1f0 LoadLibraryA 0x40d1f4 GetProcAddress 0x40d1f8 ExitProcess ADVAPI32.dll 0x40d200 RegOpenKeyA KERNEL32.DLL 0x419128 LoadLibraryA 0x41912c GetProcAddress 0x419130 ExitProcess advapi32.dll 0x419138 RegCloseKey oleaut32.dll 0x419140 SysFreeString

最满意答案

不是一个完美的方法,但我实施了一个解决方法,这应该做:)

import sys value = sys.argv[1] print value existing_dlls = [] with open("final.txt", "r") as outputd: #if this file does not exists initially make(an empty file) it else File not find Error will be thrown for line in outputd: if line.lower().find(".dll")> -1: existing_dlls.append(line.strip().lower()) dontwrite = False # Flag to keep track of address to be written with open("final.txt", "a") as outputd: #dont open this file for every write, rather keep it open for write operations with open(value) as inputd: # Parse file for line in inputd: #dont iterate again and again for similar checks, if statements are mutually exclusive if line.strip() == 'Imports': continue elif line.strip() == 'Exports': continue elif "none" not in line: if line.strip().lower().endswith(".dll"): if line.strip().lower() in existing_dlls: dontwrite = True else : dontwrite = False if not dontwrite: outputd.write(line)

Not a perfect way to do it, but I implemented a workaround, This should do :)

import sys value = sys.argv[1] print value existing_dlls = [] with open("final.txt", "r") as outputd: #if this file does not exists initially make(an empty file) it else File not find Error will be thrown for line in outputd: if line.lower().find(".dll")> -1: existing_dlls.append(line.strip().lower()) dontwrite = False # Flag to keep track of address to be written with open("final.txt", "a") as outputd: #dont open this file for every write, rather keep it open for write operations with open(value) as inputd: # Parse file for line in inputd: #dont iterate again and again for similar checks, if statements are mutually exclusive if line.strip() == 'Imports': continue elif line.strip() == 'Exports': continue elif "none" not in line: if line.strip().lower().endswith(".dll"): if line.strip().lower() in existing_dlls: dontwrite = True else : dontwrite = False if not dontwrite: outputd.write(line)

更多推荐

本文发布于:2023-08-06 13:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件   内容   group   text   based

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!