将标题打印到文件一次

时间:2017-06-09 15:30:21

标签: python-3.x

 def x(filename, saved):
    with open(filename, 'r') as fin, \
         open(saved, 'w') as fout:
        header = next(fin, '\n')
        for line in fin:
            line = line.rstrip('\n')
            fields = line.split(',')
            fields_new = header + line
            if float(fields[1]) <= float(fields[2]):
                print(fields_new, file=fout)

问题是当我打印时,我每次都会重复标题,如下所示:

header
line
header
line
etc..

而不是

header
line
line
line

如何才能让标题只显示一次?

1 个答案:

答案 0 :(得分:0)

public class VirusPool {

    public ExecutorService executor;
    public ArrayList<Runnable> tasks = new ArrayList<>();

    public VirusPool(LifeProcessor lp, boolean[][] gameBoard, 
        ArrayList<Point> point, ReentrantLock lock, ArrayList<Point> survivingCells){
        int threads = Runtime.getRuntime().availableProcessors();
        executor = Executors.newFixedThreadPool(threads);  
        for (int k = 0; k < threads; k++){  
            Runnable worker = new VirusThread(lp,gameBoard,point,lock,survivingCells,k+1,8);
            tasks.add(worker);
        }              
    }

    public void start(){
        for (Runnable task : tasks){
            executor.execute(task);
        }
        executor.shutdown();  
        while (!executor.isTerminated()) {   }  
    }}