0

我在 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("|",""));

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

球球

4

1 回答 1

0

这是正确的,但请记住原始字符串没有更改。但是,Genes.add 将包含带有 | 的字符串 替换你也可以为 & 做同样的事情。

于 2013-06-06T19:49:09.400 回答