[英]Need explanation some terms in python code
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 我需要知道while(line)
如何变为真以及下一行发生了什么
f2.write('{},{},{},{}\n'.format(data[0],data[1],data[2],total))
In your example, line is a string. 在您的示例中,line是一个字符串。 In python, a string will be evaluated as False if it's an empty string, say ''
. 在python中,如果字符串为空字符串,则将其评估为False,例如''
。 Otherwise, it will be evaluated as True. 否则,它将被评估为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
. f2.write('{},{},{},{}\\n'.format(data[0],data[1],data[2],total))
作用是将四个{}
填充为格式函数中给定的相应值,然后将其写出到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
. 例如, f2.write('{},{},{},{}\\n'.format('Nimal', 1, 2, 3))
将被形式化为字符串'Nimal,1,2,3\\n'
不带引号),然后将此字符串写入文件f2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.