Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am having the below data file in the text format:

 Proto  Local Address          Foreign Address        State           PID

  TCP    0.0.0.0:11             0.0.0.0:0              LISTENING       12   dns.exe

  TCP    0.0.0.0:95             0.0.0.0:0              LISTENING       589  lsass.exe

  TCP    0.0.0.0:111            0.0.0.0:0              LISTENING       888  svchost.exe

  TCP    0.0.0.0:123            0.0.0.0:0              LISTENING       123  lsass.exe

  TCP    0.0.0.0:449            0.0.0.0:0              LISTENING       2    System

From that, I just want to extract the names of the process ID column such as dns.exe, lsass.exe, etc... How can I achieve that?

1 Answer

0 votes
by (107k points)

I think, in your case, the split() method should work just fine so long you neglect the header column value in your data frame:

processes = []

with open("file.txt", "r") as f:

    lines = f.readlines()

    # Loop through all lines, ignoring header.

    # Add last element to list (i.e. the process name)

    for l in lines[1:]:

        processes.append(l.split()[-1])

print processes

The output:

['dns.exe', 'lsass.exe', 'svchost.exe', 'lsass.exe', 'System']

For more information regarding the same, kindly refer to the Python training course. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...