write a code to find biggest value and print the line output

How to write python code that would find a biggest value from a file and print the line that hold that big value

example:
file.txt
10 172.1.1.1
200 172.1.1.2
3  172.1.1.3

The script should loop through file.txt find and select number 200 and print 200 172.1.1.2 to a file.

I have the following, but it only print the first number 200

max_num = 0
with open('file.txt', 'r') as data: # use the with context so that the file closes gracefully
  for line in data.readlines(): # read the lines as a generator to be nice to my memory

    val = int(line.split()[0])
    if val > max_num: # logic
     max_num = val
print max_num #result
rawandnetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Try this.

max_num = 0
with open('file.txt', 'r') as data: # use the with context so that the file closes gracefully
  for line in data.readlines(): # read the lines as a generator to be nice to my memory

    val = int(line.split()[0])
    if val > max_num: # logic
     max_num = val
     max_line = line

print max_line #result

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
Or use the max() function
with open('c:\users\mark\downloads\Q_29099696.txt') as f:
    print max(f.readlines(),key=lambda d: int(d.split(' ')[0]))

Open in new window

Note: this is 2.7 code.  The print statement is a function in version 3.
0
rawandnetAuthor Commented:
Straight forward and easy solution.  Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.