Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:03+00:00 2026-06-13T11:39:03+00:00

this is my first question asked here at stackoverflow and am really looking forward

  • 0

this is my first question asked here at stackoverflow and am really looking forward to being part of this community. I am new to program and python was the most recommended first program by many people.

Anyways. I have a log file which looks like this:

"No.","Time","Source","Destination","Protocol","Info"
"1","0.000000","120.107.103.180","172.16.112.50","TELNET","Telnet Data ..." 
"2","0.000426","172.16.112.50","172.16.113.168","TELNET","Telnet Data ..." 
"3","0.019849","172.16.113.168","172.16.112.50","TCP","21582 > telnet [ACK]" 
"4","0.530125","172.16.113.168","172.16.112.50","TELNET","Telnet Data ..." 
"5","0.530634","172.16.112.50","172.16.113.168","TELNET","Telnet Data ..."

And I wanted to parse the log file using Python to make it look like this as the result:

From IP 135.13.216.191 Protocol Count:
(IMF 1)
(SMTP 38)
(TCP 24) (Total: 63)

I would really like some help on what path to take to tackle this problem should I use lists and loop through it or dictionaries/tuples?

Thanks in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:39:04+00:00Added an answer on June 13, 2026 at 11:39 am

    First you’ll want to read in the text file

    # Open the file
    file = open('log_file.csv')
    # readlines() will return the data as a list of strings, one for each line
    log_data = file.readlines()
    # close the log file
    file.close()
    

    Set up a dictionary to hold your results

    results = {}
    

    Now iterate over your data, one line at a time, and record the protocol in the dictionary

    for entry in log_data:
        entry_data = entry.split(',')
        # We are going to have a separate entry for each source ip
        # If we haven't already seen this ip, we need to make an entry for it
        if entry_data[2] not in results:
            results[entry_data[2]] = {'total':0}
        # Now check to see if we've seen the protocol for this ip before
        # If we haven't, add a new entry set to 0
        if entry_data[4] not in results[entry_data[2]]:
             results[entry_data[2]][entry_data[4]] = 0
        # Now we increment the count for this protocol
        results[entry_data[2]][entry_data[4]] += 1
        # And we increment the total count
        results[entry_data[2]]['total'] += 1
    

    Once you’ve counted everything, just iterate over your counts and print out the results

    for ip in results:
        # Here we're printing a string with placeholders. the {0}, {1} and {2} will be filled
        # in by the call to format
        print "from: IP {0} Protocol Count: {1})".format(
            ip,
            # And finally create the value for the protocol counts with another format call
            # The square braces with the for statement inside create a list with one entry
            # for each entry, in this case, one entry for each protocol
            # We use ' '.join to join each of the counts with a string
            ' '.join(["({0}: {1})".format(protocol, results[ip][protocol] for protocol in results[ip])]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first question asked here at stackoverflow, so bear with me please
FIRST: I realize this question has been asked here: in ExtJS, is it better
Since this is my first question here on stackoverflow I hope my question is
This is the first question I have asked here so bear with me .
This question completes the first one that I asked here about how to change
I know this question has been asked and answered here on StackOverflow.. but I
I asked same question on stackoverflow . First, here is a picture of what
this is my first question in here, and I would like to ask if
this is my first question here so be gentle ! recently i try to
this is my first question on here. The question is simple - # this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.