Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 只需将标题打印到文件一次_Python 3.x - Fatal编程技术网

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)