【问题标题】:Getting Duplicate while trying to read CSV file with Apache Common CSV尝试使用 Apache Common CSV 读取 CSV 文件时出现重复
【发布时间】:2018-07-31 21:56:41
【问题描述】:

我有一个类尝试使用 Apache Common CSV 读取 CSV 文件,到目前为止,我的代码工作正常,只是没有得到预期的结果。 我的代码在 csv 文件中显示了第二列的副本,如下所示:

support@gmail.com
google
google.com
support@gmail.com
google
tutorialspoint
info@tuto.com
google

我的 CSV 文件

Name,User Name,Password
google.com,support@gmail.com,google
tutorialspoint,info@tuto.com,google

我希望得到这样的结果:

google.com
support@gmail.com
google
tutorialspoint
info@tuto.com
google

这是我使用 Apache CSV 解析 csv 的块

public List<String> readCSV(String[] fields) {
        // HERE WE START PROCESSING THE READ CSV CONTENTS
        List<String> contents = new ArrayList<String>();
        FileReader fileReader = null;
        CSVParser csvFileParser = null;

        // HERE WE START PROCESSING
        if(fields!=null){
            //Create the CSVFormat object with the header mapping
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);

            try {

                //Create a new list of student to be filled by CSV file data
                List<String> content=new ArrayList<String>();

                //initialize FileReader object
                fileReader = new FileReader(FilePath);

                //initialize CSVParser object
                csvFileParser = new CSVParser(fileReader, csvFileFormat);

                //Get a list of CSV file records
                List<CSVRecord> csvRecords = csvFileParser.getRecords(); 

                //Read the CSV file records starting from the second record to skip the header
                for (int i = 1; i < csvRecords.size(); i++) {
                    CSVRecord record = csvRecords.get(i);
                    //Create a new student object and fill his data
                    for(int j=0; j<fields.length; j++){
                        content.add(record.get(fields[j]));
                    }
                    // Here we submit to contents
                    contents.addAll(content);
                    System.out.println(contents.size());
                } // end of loop
            } 
            catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fileReader.close();
                    csvFileParser.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
            // Here we return
            return contents;
        }

我无法弄清楚这里缺少什么,欢迎任何帮助。

【问题讨论】:

  • 您的 CSV 是 CSV 还是 TSV?您发布的内容似乎是制表符分隔的...您的意思是忽略标题吗?
  • 我已经编辑了它,它的逗号分隔值 (CSV)。我在循环显示时忽略了标题
  • 对于遇到此问题的其他人,请确保在处理解析器之前不要过早关闭阅读器。我发现这样做会导致标题为我重复。

标签: java csv apache-commons-csv


【解决方案1】:

原因是您每次迭代都添加字符串列表content

                contents.addAll(content);

在每次迭代时清除 content 或者只是更改

                    content.add(record.get(fields[j]));

                    contents.add(record.get(fields[j]));

并删除

                contents.addAll(content);

【讨论】:

  • 非常感谢。我改成二维数组了,以后操作起来会更方便
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2019-05-19
  • 1970-01-01
  • 2015-02-13
  • 2013-04-03
  • 2020-07-20
  • 2018-12-26
相关资源
最近更新 更多