合并两个csv

合并两个csv

 

package com.vfsd;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

public class MergeTwoCSV {
    public static void main(String[] args) throws IOException {
        
        
    
        String inputCsvFileName1 = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_1_8.csv";
        String inputCsvFileName2 = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_75_6.csv";
        
        String outputCsvFileName = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_7_1.csv";
        readCSVAndWrite(inputCsvFileName1,inputCsvFileName2,outputCsvFileName);
        
    }
    
    /**
     * 读取CSV文件内容
     * @param csvFileName
     * @throws IOException
     */
    public static void readCSVAndWrite(
            String inputCsvFileName1,
            String inputCsvFileName2,
            String outputCsvFileName) throws IOException{
        try {
            // 创建CSV读对象
            CsvReader csvReader1 = new CsvReader(inputCsvFileName1);
            CsvReader csvReader2 = new CsvReader(inputCsvFileName2);
            // 读表头
            csvReader1.readHeaders();
            csvReader2.readHeaders();
            
            
            CsvWriter csvWriter1 = new CsvWriter(outputCsvFileName,',', Charset.forName("UTF-8"));
            
            String[] headers2 = {"id","XW1","XW2","XW3","XW4","XW5","XW6","XW7","XW8"};
            csvWriter1.writeRecord(headers2);
            
            
            //csvReader1.readRecord();
            int n=1;
            while (csvReader1.readRecord()){
                //System.out.println(csvReader1.getRawRecord());
                
                //System.out.println(csvReader1.getRawRecord());
                
                //for(int k=0;k<list_headers.size();k++) {
                //    String indexHeader = list_headers.get(k);
                //    String cell_value_str = csvReader.get(indexHeader);
                //    cell_value = cell_value+Double.parseDouble(cell_value_str);
                //}
                String[] content2 = new String[headers2.length];
                
                double cell1_value = 0.0;
                double cell2_value = 0.0;
                double cell3_value = 0.0;
                double cell4_value = 0.0;
                
                double cell5_value = 0.0;
                double cell6_value = 0.0;
                double cell7_value = 0.0;
                double cell8_value = 0.0;
                
                String cell1_value_str1 = csvReader1.get(headers2[1]);
                String cell2_value_str1 = csvReader1.get(headers2[2]);
                String cell3_value_str1 = csvReader1.get(headers2[3]);
                String cell4_value_str1 = csvReader1.get(headers2[4]);
                
                String cell5_value_str1 = csvReader1.get(headers2[5]);
                String cell6_value_str1 = csvReader1.get(headers2[6]);
                String cell7_value_str1 = csvReader1.get(headers2[7]);
                String cell8_value_str1 = csvReader1.get(headers2[8]);
                
                
                if(csvReader2.readRecord()) {
                    String cell1_value_str2 = csvReader2.get(headers2[1]);
                    String cell2_value_str2 = csvReader2.get(headers2[2]);
                    String cell3_value_str2 = csvReader2.get(headers2[3]);
                    String cell4_value_str2 = csvReader2.get(headers2[4]);
                    
                    String cell5_value_str2 = csvReader2.get(headers2[5]);
                    String cell6_value_str2 = csvReader2.get(headers2[6]);
                    String cell7_value_str2 = csvReader2.get(headers2[7]);
                    String cell8_value_str2 = csvReader2.get(headers2[8]);
                    
                    cell1_value = Double.parseDouble(cell1_value_str1) + Double.parseDouble(cell1_value_str2);
                    cell2_value = Double.parseDouble(cell2_value_str1) + Double.parseDouble(cell2_value_str2);
                    cell3_value = Double.parseDouble(cell3_value_str1) + Double.parseDouble(cell3_value_str2);
                    cell4_value = Double.parseDouble(cell4_value_str1) + Double.parseDouble(cell4_value_str2);
                    
                    cell5_value = Double.parseDouble(cell5_value_str1) + Double.parseDouble(cell5_value_str2);
                    cell6_value = Double.parseDouble(cell6_value_str1) + Double.parseDouble(cell6_value_str2);
                    cell7_value = Double.parseDouble(cell7_value_str1) + Double.parseDouble(cell7_value_str2);
                    cell8_value = Double.parseDouble(cell8_value_str1) + Double.parseDouble(cell8_value_str2);
                    
                }else {
                    cell1_value = Double.parseDouble(cell1_value_str1) ;
                    cell2_value = Double.parseDouble(cell2_value_str1) ;
                    cell3_value = Double.parseDouble(cell3_value_str1) ;
                    cell4_value = Double.parseDouble(cell4_value_str1) ;
                    
                    cell5_value = Double.parseDouble(cell5_value_str1) ;
                    cell6_value = Double.parseDouble(cell6_value_str1) ;
                    cell7_value = Double.parseDouble(cell7_value_str1) ;
                    cell8_value = Double.parseDouble(cell8_value_str1) ;
                }
                
                
                content2[0] = n+"";
                content2[1] = cell1_value+"";
                content2[2] = cell2_value+"";
                content2[3] = cell3_value+"";
                content2[4] = cell4_value+"";
                
                content2[5] = cell5_value+"";
                content2[6] = cell6_value+"";
                content2[7] = cell7_value+"";
                content2[8] = cell8_value+"";
                
                csvWriter1.writeRecord(content2);
                // 读一整行
                //System.out.println(csvReader.getRawRecord());
                // 读这行的某一列
                //System.out.println(csvReader.get("A")+"\t"+csvReader.get("B")+"\t"+csvReader.get("C"));
                
                n++;
            }
            
            csvWriter1.flush();
            csvWriter1.close();
            
            csvReader1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println("finish...");
        }
    }
    
    
    
    
    

}

 

 

 

####################

posted @ 2022-04-29 21:39  西北逍遥  阅读(256)  评论(0编辑  收藏  举报