我有带有标题和特定数字的 CSV 文件。我需要单独提取这些数字来创建一个 AIM XML 文件,但这并不重要。我很难根据标题提取数字。我尝试了各种方法,但无法使其正常工作。当我尝试运行此代码(以下只是一个示例)时,它似乎永远不会终止。我对 Java 或 OpenCSV 不是很有经验,所以请原谅你即将看到的混乱。(现在也是凌晨 2 点,我的脑子出了问题)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import edu.stanford.hakan.aim3api.base.AimException;
import edu.stanford.hakan.aim3api.base.ImageAnnotation;
import edu.stanford.hakan.aim3api.usage.AnnotationBuilder;
import au.com.bytecode.opencsv.CSVReader;
public class CreateAIMFile {
public static void main(String[] args) throws IOException, FileNotFoundException, AimException
{
ImageAnnotation PeserAnnotation1 = new ImageAnnotation ();
String csvFilename = "C:/Users/Daniela/Documents/ISPY/205_4493_MR1_ACRIN_6657/E25008/peser_data.csv"; //Insert file name and location
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String [] nextLine = csvReader.readNext();
//start
for (int i = 0; i<50; i++){ //i<50 is a random number
while (nextLine[i] != null){
if (nextLine[i] == "CagridId") //customize name
{
CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ';', '\'', 1);
Integer A = Integer.valueOf(nextLine[i]);
PeserAnnotation1.setCagridId(A);
}
else
{
PeserAnnotation1.setCagridId(0);
}
}
}
for (int i = 0; i<50; i++){
while (nextLine[i] != null){
if (nextLine[i] == "PatientComment") //customize name
{
CSVReader csvreader = new CSVReader(new FileReader(csvFilename), ';', '\'', 1);
PeserAnnotation1.setComment(nextLine[i]);
}
else
{
PeserAnnotation1.setComment("");
}
}
}
AnnotationBuilder.saveToFile(PeserAnnotation1, "./AIMFileName.xml", "AIM_v3.xsd"); //customize name of XML file
System.out.println(AnnotationBuilder.getAimXMLsaveResult());
}
}