2017-11-03 63 views
1

所以,大家好,这是我的第一个问题。事实上,在找了一段时间后,我没有找到有用的东西。我对Java和数据挖掘颇为陌生。 我有一个问题,所以我想从CSV文件中获取一些数据以将其转换为ARFF(这将允许我在Weka中正确打开它) 所以问题是我拥有的CSV有点破,我有2个属性,一个是情绪,一个是像素,但事实是像素是由17个不同的像素值构成的。 我打算把我的第一个属性放在List的ArrayList中,每个列表都由一个元素组成,代表一种情绪。 另一个列表的列表,其中每个列表是我的17个像素的一堆我不知道我是否清楚,但这里是我的代码后跟我的输出和我想要的输出。Java:使用ArrayList的Integer列表CSV读取

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.nio.charset.Charset; 
import java.nio.file.Files; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class CSVformat { 

    private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>(); 
    private static ArrayList<List<Integer>> Pixels = new ArrayList<List<Integer>>(); 

    public CSVformat(ArrayList<List<Integer>> emotions, ArrayList<List<Integer>> pixels){ 
    this.setEmotions(emotions); 
    this.setPixels(pixels); 
    } 

    /** 
    * @return the emotions 
    */ 
    public ArrayList<List<Integer>> getEmotions() { 
     return Emotions; 
    } 

    /** 
    * @param emotions the emotions to set 
    */ 
    public void setEmotions(ArrayList<List<Integer>> emotions) { 
     CSVformat.Emotions = emotions; 
    } 

    /** 
    * @return the pixels 
    */ 
    public ArrayList<List<Integer>> getPixels() { 
     return Pixels; 
    } 

    /** 
    * @param pixels the pixels to set 
    */ 
    public void setPixels(ArrayList<List<Integer>> pixels) { 
     CSVformat.Pixels = pixels; 
    } 

    /*public void readData(String fileName) throws IOException { 
     int count = 0; 
     String file = fileName; 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line = ""; 
      while ((line = br.readLine()) != null) { 

       Emotions.add(line.split(",")); 

       String[][] v = (String[][]) bank.toArray(new String[bank.size()][12]); 

      } 
     } catch (FileNotFoundException e) { 

     } 
    }*/ 

    public static void fillArrayList(String fileName) throws FileNotFoundException { 
     List<String> list = new ArrayList<String>(); 
     List<Integer> emotions = new ArrayList<Integer>(); 
     List<Integer> pixels = new ArrayList<Integer>(); 

     File file = new File(fileName); 
     //Scanner scan = new Scanner(new File(fileName)); 
     if(file.exists()){ 
      try { 
       list = Files.readAllLines(file.toPath(),Charset.defaultCharset()); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      if(list.isEmpty()) 
       //scan.close(); 
       return; 
     } 
     for(String line : list.subList(1, list.size())){ 
     String[] res = line.split(","); 
     String[] arr= res[1].split(" "); 
     emotions.add(Integer.parseInt(res[0])); 
     for(int i=0;i<arr.length;i++){ 
      pixels.add(Integer.parseInt(arr[i])); 

      } 

     } 
    Emotions.add(emotions); 
    Pixels.add(pixels); 
    System.out.println(Emotions); 
    System.out.println(Pixels); 
    }} 

因此,这里是我使用的是缩短CSV:

emotion,pixels 
0,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 
1,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 
3,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 

(请注意,CSV,我告诉你的是那种坏了,这是给定一个我的课程,这就是为什么我我想先解决它)

最后通过运行该代码,这个CSV我有这样的输出:

[[0, 1, 3]] 
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]] 

虽然我想要:

[[0], [1], [3]] 
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]] 

我希望我很清楚。 我知道这只是因为我使用add方法列表,但我没有设法完成此任务,甚至通过使用扫描仪逐行阅读或其他一些方法。而且我还试图在将它重新放入ArrayList之前清除列表,但它恰好给出了完全空的ArrayList。

非常感谢您的帮助。

+0

为什么你被解析自己呢?使用OpenCSV库。 – m0skit0

+0

嗨m0skit0, 这不是这个任务的目的,我必须避免使用它,如果我可以的话。 –

回答

3

你有名单

private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>(); 

的列表,你读你的文件到列表

List<Integer> emotions = new ArrayList<Integer>(); 

并将其存储到列表清单

Emotions.add(emotions); 

,如果你的打印你会得到:

[[0, 1, 3]] 

,如果你想拥有它不同,你必须创建你的List<Integer> emotions为每个环路/线路您处理,所以最终你fillArrayList方法可能如下:

public static void fillArrayList(String fileName) throws FileNotFoundException { 
    List<String> list = new ArrayList<String>(); 
    File file = new File(fileName); 
    if (file.exists()) { 
     try { 
      list = Files.readAllLines(file.toPath(), Charset.defaultCharset()); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
     if (list.isEmpty()) 
      return; 
    } 

    for (String line : list.subList(1, list.size())) { 
     System.out.println(line); 

     String[] res = line.split(","); 
     String[] arr = res[1].split(" "); 

     List<Integer> emotions = new ArrayList<Integer>(); 
     emotions.add(Integer.parseInt(res[0])); 
     Emotions.add(emotions); 

     List<Integer> pixels = new ArrayList<Integer>(); 
     for (int i = 0; i < arr.length; i++) { 
      pixels.add(Integer.parseInt(arr[i])); 
     } 
     Pixels.add(pixels); 
    } 
} 
+0

非常感谢,我现在看起来很清楚,而且我感觉有点愚蠢,以至于我以前没有看到它。 谢谢你们俩:) –