【问题标题】:How to remove special characters from with in a column of a csv file in java如何从java中的csv文件列中删除特殊字符
【发布时间】:2013-11-02 12:05:18
【问题描述】:

我的 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 以我不想要的逗号转义。请帮我解决以下问题。

【问题讨论】:

    标签: java csv bufferedreader


    【解决方案1】:
        String str = "coffee service,\"San Antonio, TX Metro\"";
        System.out.println("str = " + str.replaceAll("\"","")
                                         .replaceAll(", "," ")
                                         .replaceAll(" ","+"));
    

    输出:

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

    【讨论】:

    • 我如何首先获得 str ?请改进您的答案。
    • @user2602860 你有它在line 因为你在做:while((line=br.readLine())!=null){...
    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多