1

I'm the following log file structure. I want to find out the maximum response time and want to print the log file which is having the highest response time(R.T) using python 2.7.11.

The structure of my log file:

00.00.00.000 - - [dd/mm/yyyy:hr:mm:se +0800] GET Url HTTP/1.1 200 dataconsumed R.T
00.00.00.000 - - [dd/mm/yyyy:hr:mm:se +0800] GET Url HTTP/1.1 200 dataconsumed R.T
00.00.00.000 - - [dd/mm/yyyy:hr:mm:se +0800] GET Url HTTP/1.1 200 dataconsumed R.T

Code Used:

file =open(r"log.txt","r")
for line in file:
line_array = line.split(" ")
print line_array[10]

OUTPUT:

R.T
R.T
R.T

Until now I could able to print all the response time from the log file. I couldn't able to get the highest response time (R.T) out of it.

Help me to find the highest response time with the whole log file printed as an output.

3 Answers 3

1

Staying close to your own code and assuming it indeed gives the output as you describe:

file =open(r"log.txt","r")
highest = -1
for line in file:
    line_array = line.split(" ")
    highest = max (highest, float (line_array[10]))

print (highest)
file.close ()
Sign up to request clarification or add additional context in comments.

Comments

0

Start with an empty list BEFORE your loop

response_times = []

Instead of just printing the line element, add it to this list WITHIN the loop:

response_times.append(line_array[10])

Finally, print the maximum AFTER /outside the for loop:

print max(response_times)

Comments

0

You should get the actual response time (R.T) that will be in the R.T label (I guess), save it and then get the max one. So your code should look like:

file = open(r"log.txt","r")
rts = []
for line in file:
    line_array = line.split(" ")
    rts.append(float(line_array[10]))

#Now find max
max_rt = max(rts)
print "Max R.T is :", max_rt
file.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.