0

this is my code

'''Nimal,30,60
   Saman,80,45
   Upali,100,80
   above text is in input.txt
   '''
f1 = open('input.txt','r')
f2 = open('output.txt','w')
line = f1.readline()
while(line):
    data = (line.strip()).split(',')
    total = float(data[1])+float(data[2])
    f2.write('{},{},{},{}\n'.format(data[0],data[1],data[2],total))
    line=f1.readline()
f1.close()
f2.close()

I need to know while(line) how become true and what happen in below line

   f2.write('{},{},{},{}\n'.format(data[0],data[1],data[2],total))
4
  • Are you facing any errors? Don't really understand what you are asking here. Commented Jul 12, 2016 at 1:44
  • @BurhanKhalid this is an Exam question Commented Jul 12, 2016 at 1:46
  • So what is the question? I mean, are you asking what that line of code is doing? Its just writing data to the output file. Commented Jul 12, 2016 at 1:47
  • @Burhan Khalid dont comment as such a stupid Commented Jul 12, 2016 at 1:50

1 Answer 1

1

In your example, line is a string. In python, a string will be evaluated as False if it's an empty string, say ''. Otherwise, it will be evaluated as True.

What f2.write('{},{},{},{}\n'.format(data[0],data[1],data[2],total)) does is to fill the four {} with the corresponding values given in the format function, and then write out to f2. For example, f2.write('{},{},{},{}\n'.format('Nimal', 1, 2, 3)) will be formalized as a string 'Nimal,1,2,3\n' without quotes, and then write this string into file f2.

Sign up to request clarification or add additional context in comments.

1 Comment

Thnx I got it buddy

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.