Python 3.x 只需将标题打印到文件一次
而不是Python 3.x 只需将标题打印到文件一次,python-3.x,Python 3.x,而不是 header line header line etc.. 如何使标题只显示一次?def x(文件名,已保存): header line line line 以open(filename,'r')作为fin\ 打开(保存为“w”)作为fout: 标题=下一个(fin,“\n”) 打印(页眉,文件=fout) 对于fin中的行: line=line.rstrip('\n') 字段=行。拆分(“,”) 如果浮动(字段[1]),而不是在循环中打印“字段”\u new“打印”字段。那么,不要
header
line
header
line
etc..
如何使标题只显示一次?def x(文件名,已保存):
header
line
line
line
以open(filename,'r')作为fin\
打开(保存为“w”)作为fout:
标题=下一个(fin,“\n”)
打印(页眉,文件=fout)
对于fin中的行:
line=line.rstrip('\n')
字段=行。拆分(“,”)
如果浮动(字段[1]),而不是在循环中打印“字段”\u new“打印”字段。那么,不要在每个循环中将标题添加到字段”\u new
,在开始循环其他行之前只打印一次。
header
line
line
line
def x(filename, saved):
with open(filename, 'r') as fin, \
open(saved, 'w') as fout:
header = next(fin, '\n')
print(header, file=fout)
for line in fin:
line = line.rstrip('\n')
fields = line.split(',')
if float(fields[1]) <= float(fields[2]):
print(line, file=fout)