0

我有 CSV 文件,其中包含用逗号分隔的 6 列:

VendorCode, VendorName, Material, MaterialDescription, Reason, Quantity    

我能够从除“原因”列之外的每一列中检索数据,因为它包含该列中的值:

Pitted, Rusty     

(我的意思是该列中有逗号分隔值)

(因此 Pitted 值被插入到“Reason”arraylist 中,Rusty 被插入到“Quantity”arraylist 中对于该特定行)

例如

一行 csv 文件包含:

AA90,ABC LTD.,2.71E+11,ASSY/LAM`E'CE-MSSL-RINDER,"VisualCrack,BH,Damage,Burr",330

DESIRED OUTPUT : "pitty,rusted" 应被视为单个字符串并存储在数据库中

我试过这个

while ((thisLine = myInput2.readLine()) != null)  {

    String[] str = thisLine.split(",");
    for(int j=0; j<str.length; j++) {
        switch(j) {
            case 0: DR_VendorCode.add(str[j]);//getContents()); break;
            case 1:DR_VendorName.add(str[j]);//getContents()); break;
            ....
        }

        i1++;
    }
}

for(int k=0;k<DR_VendorCode.size()+1;k++) {

    String DR_VCODE=DR_VendorCode.get(k).toString();
    String DR_VNAME=DR_VendorName.get(k).toString();
    ....
}
4

3 回答 3

0

我建议您搜索包含的模式

"  ,   " // (make sure multiple commas could also occur)

即双引号,1个或多个逗号,双引号。

  1. 并将其中的逗号替换为一个符号左右。
  2. 然后随意替换所有逗号;
  3. 将您的符号替换回逗号。

这是部分伪代码!我不知道如何替换模式,但你会发现。

String inlineCommasReplaced = thisLine.replace( \"\,\", \"\|\"); // replace pattern "," with "|"
String outterCommasReplaced = inlineCommasReplaced.replace(",", ",,"); // Replace the comma seperated with double commas for instance
String replaceBackInlineCommas = outterCommasReplaces.replace( \"|,\", \"\,\"); // replace back the "|" to ","
String[] str = replaceBackInlineCommas.split(",,"); // split on ,,
于 2012-12-04T10:08:30.847 回答
0

Here you go ...

String regex = "\"[^\"]+\"|[^,]+";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(thisLine);
while(m.find()) {
    System.out.println(thisLine.substring(m.start(),m.end()));
    // I suppose you could construct an array here rather than just printing it
}

Output ...

AA90
ABC LTD.
2.71E+11
ASSY/LAM`E'CE-MSSL-RINDER
"VisualCrack,B.H,Damage,Burr"
330

To handle 5th (actually any but only one) column with multiple embedded quotes ...

String regex = "\".*\"|[^,]+";

Output ...

AA90
ABC LTD.
2.71E+11
ASSY/LAM`E'CE-MSSL-RINDER
"VisualCrack,B.H,"Damage",Burr"
330
于 2012-12-04T12:47:51.370 回答
0

终于在@xagyg 的帮助下解决了问题

最终代码是:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
//import org.apache.commons.collections.*;
//import org.apache.commons.collections.MultiMap;
//import org.apache.commons.collections.MultiHashMap;
import java.util.regex.Pattern;

public class readFileandPopulate{

  public static void main(String[] args) 
  {
      ArrayList<String> total=new ArrayList<String>();

     ArrayList<String> vcode=new ArrayList<String>();
      ArrayList<String> vname=new ArrayList<String>();
      ArrayList<String> material=new ArrayList<String>();
      ArrayList<String> mat_desc=new ArrayList<String>();
      ArrayList<String> reason=new ArrayList<String>();
      ArrayList<String> quantity=new ArrayList<String>();

      try
      {
          FileInputStream fstream1 = new FileInputStream("C:\\filesuploaded\\DEVELOPMENT_REJECTION.csv");
          DataInputStream in1 = new DataInputStream(fstream1);
          BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
          String strLine1;
          while ((strLine1 = br1.readLine()) != null)  
          {
              String regex = "\"[^\"]+\"|[^,]+";
              Pattern p = Pattern.compile(regex);
              Matcher m = p.matcher(strLine1);
              while(m.find()) {
                  total.add(strLine1.substring(m.start(),m.end()));
                  }
          }
          for(int i = 0; i < total.size(); i++)
          {
              if(i % 6 == 0)
                  vcode.add(total.get(i));
              else if(i % 6 == 1)
                  vname.add(total.get(i));
              else if(i % 6 == 2)
                  material.add(total.get(i));
              else if(i % 6 == 3)
                  mat_desc.add(total.get(i));
              else if(i % 6 == 4)
                  reason.add(total.get(i));
              else if(i % 6 == 5)
                  quantity.add(total.get(i));
          }
          System.out.println("vcode content: "+vcode);
          System.out.println("vname content: "+vname);
          System.out.println("material content: "+material);
          System.out.println("mat_desc content: "+mat_desc);
          System.out.println("reason content: "+reason);
          System.out.println("quantity content: "+quantity);      
          in1.close();

      }
      catch (Exception e)
          {     e.printStackTrace();
                System.err.println("Error: " + e.getMessage());
          }

  }

}
于 2012-12-05T04:11:00.803 回答