简体   繁体   中英

Extract specific part of .txt file using python

I am relatively new to 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).

突出显示所需信息的快照

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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