简体   繁体   English

使用python提取.txt文件的特定部分

[英]Extract specific part of .txt file using python

I am relatively new to Python.我对 Python 比较陌生。 I am trying to extract a particular part (column) of the text file.我正在尝试提取文本文件的特定部分(列)。 I am failing to get the output I expect.我没有得到我期望的输出。

Text file is as follows (hope the newline and carriage returns will be accessible)文本文件如下(希望换行和回车可以访问)

000022E4                                                                    ST0=FFFFFFFFFFFFFFFF ST1=FFFFFFFFFFFFFFFF ST2=FFFFFFFFFFFFFFFF ST3=FFFFFFFFFFFFFFFF ST4=FFFFFFFFFFFFFFFF ST5=FFFFFFFFFFFFFFFF ST6=FFFFFFFFFFFFFFFF ST7=FFFFFFFFFFFFFFFF CTRL=FFFF CS=0023 DS=002B ES=002B FS=0053 GS=002B SS=002B EAX=00000001 EBX=0063CC4C ECX=80049550 EDX=00000000 ESI=8004959F EDI=800495AB EBP=0063CD18 ESP=0063CC2C EFL=00000246 XMM0= XMM1= XMM2= XMM3= XMM4= XMM5= XMM6= XMM7= MXCSR=FFFFFFFF MM0= MM1= MM2= MM3=   
000022E4    .text:main                  push    ebp                         ESP=0063CC28                                
000022E4    .text:main+1                mov     ebp, esp                    EBP=0063CC28                                
000022E4    .text:main+3                and     esp, 0FFFFFFF0h             ESP=0063CC20 PF=0 ZF=0                      
000022E4    .text:main+6                call    __main                      ESP=0063CC1C                                

The extracted result shall be as follows :提取结果如下:

push
mov 
and 
call

I would love to see what would be the best way to extract this information from given text file.我很想知道从给定的文本文件中提取这些信息的最佳方法是什么。

(This is an example snippet, the actual text file is with huge number of file but with same format) (这是一个示例片段,实际的文本文件具有大量文件但格式相同)

NOTE: Luckily, if the text file content from the query here is copy pasted to Notepad++ or any editor, the actual file structure can be seen (along with Newline and Carriage return).注意:幸运的是,如果将此处查询中的文本文件内容复制粘贴到 Notepad++ 或任何编辑器,则可以看到实际的文件结构(以及换行符和回车符)。

突出显示所需信息的快照

Regex doesn't seem appropriate here since your text file appears to use fixed-width columns.正则表达式在这里似乎不合适,因为您的文本文件似乎使用固定宽度的列。 Use a slice to extract the column, trim whitespace and disregard any empty rows:使用切片提取列,修剪空格并忽略任何空行:

result = []

with open("file.txt", "r") as f:
    for line in f:
        instruction = line[40:48].strip()

        if instruction: 
            result.append(instruction)

print(result) # => ['push', 'mov', 'and', 'call']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM