在CSV文件中解析列

时间:2013-06-06 19:45:45

标签: java string

我在csv文件中有几列数据。我能够隔离我想要的列,但现在我需要删除“|”和“&”列中包含的符号。谁能帮我吗?相关代码如下:

// CSVRead.java
//Reads a Comma Separated Value file and prints its contents.

import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;

public class CSVRead{

 public static void main(String[] arg) throws Exception {

   BufferedReader b1 = 
        new BufferedReader(new FileReader("Reactions.csv"));

   BufferedWriter b2 = 
        new BufferedWriter(new FileWriter("Write1.txt"));

   BufferedWriter b3 = 
        new BufferedWriter(new FileWriter("Write2.txt"));

   Pattern p1 = Pattern.compile("[+]");
   Pattern p3 = Pattern.compile("\t");
   Pattern p2 = Pattern.compile("<=>");
   Pattern p4 = Pattern.compile(" ");
   Pattern p5 = Pattern.compile("RSP_[0-9][0-9][0-9][0-9]");

   ArrayList<String> reactions = new ArrayList<String>();//List to hold all reactions
   ArrayList<String> reactionID = new ArrayList<String>();//List to hold all reactions
   ArrayList<String> genes = new ArrayList<String>();//List to hold all genes

   String strRead; String strWrite;

   while ((strRead=b1.readLine())!=null) {
     String splitarray[] = strRead.split("\t");
     String firstentry = splitarray[0];
     String secondentry = splitarray[7];
     b3.write(secondentry);
     b3.newLine();
   } 

   b2.close(); 
   b3.close();

 } //main()

} // CSVRead

我想我可能不得不使用表达式:

genes.add(splitarray[7].replace("|",""));
例如,

。但我不知道该把它放在哪里,或者它是否正确。谢谢你的帮助,

Kbball

1 个答案:

答案 0 :(得分:0)

这是正确的,但请记住,原始字符串不会更改。然而,Genes.add将包含带有|的字符串替换你可以做同样的&amp;还