• 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 MergeCSVColsAndOut {
        public static void main(String[] args) throws IOException {
            
            
        
            String inputCsvFileName1 = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_1.csv";
            //String inputCsvFileName2 = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_75_6.csv";
            
            String outputCsvFileName = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week1\\week1_90.csv";
            //String outputCsvFileName = "F:\\PythonProject\\DoWhy_Project\\dowhy_202204\\data\\week_1_13.csv";
            readCSVAndWrite(inputCsvFileName1,outputCsvFileName);
            
        }
        
        /**
         * 读取CSV文件内容
         * @param csvFileName
         * @throws IOException
         */
        public static void readCSVAndWrite(
                String inputCsvFileName1,
                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[] headers1 = {"id","XW1","XW2","XW3","XW4","XW5","XW6","XW7","XW8","GXW1"};
                String[] headers2 = {"GXW1","GXW2","GXW3","GXW4","GXW5","GXW6","GXW7","GXW8","GXW9","GXW10","GXW11","GXW12","GXW13"};
                
                csvWriter1.writeRecord(headers1);
                
                
                //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[headers1.length];
                    
                    
                    content2[0] = n+"";
                    
                    for(int k=1;k<headers1.length-1;k++)
                    {
                        String indexHeader = headers1[k];
                        String cell1_value_str = csvReader1.get(indexHeader);
                        content2[k] = cell1_value_str;
                    }
                    
                    
                    double col1_13 = 0.0;
                    for(int k=0;k<headers2.length;k++)
                    {
                        String indexHeader = headers2[k];
                        String cell1_value_str = csvReader1.get(indexHeader);
                        col1_13 = col1_13+Double.parseDouble(cell1_value_str);
                    }
                    
                    double avg9_13 = col1_13/13;
                    content2[headers1.length-1] = avg9_13+"";
                    
                    
                    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...");
            }
        }
    
    
    }

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

  • 相关阅读:
    P3373 线段树模板
    由AC自动机引发的灵感
    瞎子摸象序
    Outer Join的where条件
    将 转移单 自动发货
    获取某个结点的所有层
    瞎子摸象库存篇
    js修改select列表选项中的值
    JS仿flash动态切换(横向,带分页器控制,自动正反向循环轮播)
    js操作select下拉列表的一些通用代码
  • 原文地址:https://www.cnblogs.com/herd/p/16197805.html
Copyright © 2020-2023  润新知