java - 如何在java中删除csv文件列中的特殊字符

标签 java csv bufferedreader

我的 CSV 文件如下所示:

我想解析以下 CSV 文件:

Entity,GeographicLocation(Headers)
coffee service,"San Antonio, TX Metro"
coffee service,"Honolulu, HI Metro"
coffee service,"Little Rock, AR Metro"
coffee service,"Jacksonville, FL Metro"
coffee service,"Lincoln, NE Metro"

我想获取此表单的字段:(所需输出)

coffee+service,San+Antonio+TX+Metro
coffee+service,Honolulu+HI+Metro
coffee+service,Little+Rock+AR+Metro and so on ...

我的代码如下所示:

     // String CSVfilename = args[0];
    String csvfile="/Users/cdas/databaseprograms/coffeeservice.csv";

     // Step 1 >
     // code to read the data from the CSV file
    BufferedReader br=new BufferedReader(new FileReader(csvfile));
    StringTokenizer st=null;
    String line="";
     int linenumber=0;
     int columnnumber;
    int free=0;
    int free1=0;

 // create the arraylist
  ArrayList<String> Typeof = new ArrayList<String>();
  // ArrayList for the adgroupId
  ArrayList<String> Where = new ArrayList<String>();

   // reading from the CSV file
   while((line=br.readLine())!=null){
    linenumber++;
    columnnumber=0;

      st=new StringTokenizer(line,",");
      while(st.hasMoreTokens()){
            columnnumber++;
            String token=st.nextToken();
            token = token.replaceAll("\"","");
            token = token.replaceAll(",","");
            System.out.println(token);
            if("Entity".equals(token)){
                    free=columnnumber;
                    System.out.println("the value of free"+free);
                    } else if("GeographicLocation".equals(token)){
                    free1=columnnumber;
                    System.out.println("the value of free1"+free1);
                    }
            if(linenumber>1){
                if (columnnumber==free){                    
                    token = token.replaceAll(" ","+");
                        Typeof.add(token);
                    } else if(columnnumber==free1){
                    //token = token.replaceAll(",","+");
                    token = token.replaceAll(" ","+");
                        Where.add(token);
                        }
    }                   
 }
 }

 // converting the  headline arraylist to array
  String[] entity=Typeof.toArray(new String[Typeof.size()]);
   for(int i=0;i<entity.length;i++){
        System.out.println(entity[i]);}

        // converting the keyword Id arraylist to array
        String[] whereof=Where.toArray(new String[Where.size()]);
        for(int i=0;i<whereof.length;i++){
        System.out.println(whereof[i]);
        }


 My Output is like : -
  Entity
the value of free1
GeographicLocation
the value of free12
coffee service
San Antonio
TX Metro
coffee service
Honolulu
HI Metro
coffee service
Little Rock
AR Metro
coffee service
Jacksonville
FL Metro
coffee service
Lincoln
NE Metro

我已成功替换双引号,但 bufferedReader 在我不想要的逗号处转义。请帮我解决以下问题。

最佳答案

    String str = "coffee service,\"San Antonio, TX Metro\"";
    System.out.println("str = " + str.replaceAll("\"","")
                                     .replaceAll(", "," ")
                                     .replaceAll(" ","+"));

输出:

str = coffee+service,San+Antonio+TX+Metro

关于java - 如何在java中删除csv文件列中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19554806/

相关文章:

java - 读取 zip 或 jar 文件而不先解压缩它

java - 使用 ReactiveCouchbaseRepository 时出现 IllegalArgumentException 期望找到类 rxSingle 的响应式(Reactive)适配器,但无法找到

java - 方法覆盖和继承与异常

python - 获取 pandas.read_csv 将空字段读取为 NaN,将空字符串读取为空字符串

使用 UTF8 编码将 Excel 转换为 CSV

python - 如何从 Python 中的两对文件中获取枢轴线?

java - 读取大文件(例如非常大的文本文档)的最佳方式

java - 单击铅笔调用监听器(primefaces 2.2.1 rowEditor)

java - install4j updater 是否支持多交付 channel (稳定版/测试版/每晚)?

python - 在python中不使用openCV读取视频文件