Java 如何在阅读CSV时删除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24079093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:03:55  来源:igfitidea点击:

how to remove double quotes while reading CSV

javaopencsv

提问by Anandv

public class CSVTeast {
  public static void main(String[] args) {

      CSVTeast obj = new CSVTeast();
        obj.run();

      }

      public void run() {

        String csvFile = "D:\text.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = "~";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                    // use comma as separator

                String[] csvRead = line.split(cvsSplitBy);




                System.out.println("Value [date= " + csvRead[5] 
                                     + " , name=" + csvRead[9]+"]");

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("Done");
      }


}

Output is

输出是

Value [date= "POLICY_CHANGE_EFFECTIVE_DATE" , name="AGENCY_NAME"]

Value [date= "2014-04-01" , name="USI INSURANCE SERVICES]--this value stated with double qoutes but not end with same . 

Expected output

预期输出

Value [date= POLICY_CHANGE_EFFECTIVE_DATE , name=AGENCY_NAME]

Value [date= 2014-04-01 , name=USI INSURANCE SERVICES] 

采纳答案by Gabriel Ruiu

You can try passing the value through the String.replace()method.

您可以尝试通过String.replace()方法传递值。

So your code would be:

所以你的代码是:

public class CSVTeast {
 public static void main(String[] args) {

  CSVTeast obj = new CSVTeast();
     obj.run();
  }
  public void run() {
    String csvFile = "D:\text.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = "~";
    try {
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            String[] csvRead = line.split(cvsSplitBy);
            System.out.println("Value [date= " + csvRead[5].replace("\"","") 
                                 + " , name=" + csvRead[9].replace("\"","")+"]");
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println("Done");
  }
}

回答by Matthew Trout

There's a nice CSV Reader for Java that will handle the mess of this for you, http://opencsv.sourceforge.net/

有一个很好的 Java CSV 阅读器可以为你处理这个烂摊子,http://opencsv.sourceforge.net/

It has a maven package if your project is maven, else you can download the JARs there.

如果你的项目是 maven,它有一个 maven 包,否则你可以在那里下载 JAR。

回答by Husman

If the qoutemarks are at the beginning of every CSV line, you can do:

如果 qoutemarks 在每个 CSV 行的开头,您可以执行以下操作:

csvRead[5].substring(1, csvRead[5].length()-1)

That will remove the first and last character of that particular string. You then need to store the results somewhere or print it out.

这将删除该特定字符串的第一个和最后一个字符。然后您需要将结果存储在某处或打印出来。

回答by zeeshan

It is also important to check if the String starts with a double quote, otherwise the code will start deleting the first character of the CSV value. I do this in my code in one of my apps, where my CSV value is coming in rowData[1] which sometimes have double quotes and sometimes it doesn't, depending upon the number of words in the value String.

检查字符串是否以双引号开头也很重要,否则代码将开始删除 CSV 值的第一个字符。我在我的一个应用程序中的代码中执行此操作,其中我的 CSV 值出现在 rowData[1] 中,有时有双引号,有时没有,具体取决于值 String 中的单词数。

String item = (String.valueOf(rowData[1].charAt(0)).equals("\"") ? rowData[1].substring(1, rowData[1].length() - 1) : rowData[1]);