How to read from particular header in opencsv?

前端 未结 5 2871

I have a csv file. I want to extract particular column from it.For example: Say, I have csv:

id1,caste1,salary,name1
63,Graham,101153.06,Abraham
103,Joseph,12245         


        
5条回答
  •  旧巷少年郎
    2021-02-20 17:39

    I had a task to remove several columns from existing csv, example of csv:

    FirstName, LastName, City, County, Zip
    Steve,Hopkins,London,Greater London,15554
    James,Bond,Vilnius,Vilniaus,03250
    

    I needed only FirstName and LastName columns with values and it is very important that order should be the same - default rd.readMap() does not preserve the order, code for this task:

            String[] COLUMN_NAMES_TO_REMOVE = new String[]{"", "City", "County", "Zip"};
            CSVReaderHeaderAware rd = new CSVReaderHeaderAware(new StringReader(old.csv));
            CSVWriter writer = new CSVWriter((new FileWriter(new.csv)),
                    CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
    
            // let's get private field
            Field privateField = CSVReaderHeaderAware.class.getDeclaredField("headerIndex");
            privateField.setAccessible(true);
            Map headerIndex = (Map) privateField.get(rd);
    
            // do ordering in natural order - 0, 1, 2 ... n
            Map sortedInNaturalOrder = headerIndex.entrySet().stream()
                    .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (oldValue, newValue) -> oldValue, LinkedHashMap::new));
    
            // let's get headers in natural order
            List headers = sortedInNaturalOrder.keySet().stream().distinct().collect(Collectors.toList());
    
            // let's remove headers
            List removedColumns = new ArrayList(Arrays.asList(COLUMN_NAMES_TO_REMOVE));
            headers.removeAll(removedColumns);
            // save column names           
            writer.writeNext(headers.toArray(new String[headers.size()]));
       
            List keys = new ArrayList<>();
            Map values;
            while ((values = rd.readMap()) != null) {
                for (String key : headers) {
                    keys.add(values.get(key));
                    if (keys.size() == headers.size()) {
                        String[] itemsArray = new String[headers.size()];
                        itemsArray = keys.toArray(itemsArray);
                        // save values                       
                        writer.writeNext(itemsArray);
                        keys.clear();
                    }
                }
            }
            writer.flush();
    

    Output:

    FirstName, LastName
    Steve,Hopkins
    James,Bond
    

提交回复
热议问题