2017-11-03 1 views
1

안녕하세요. 여기는 내 첫 번째 질문입니다. 사실 잠시 후에 나는 도움이되는 것을 찾지 못했습니다. 나는 Java와 Data Mining을 처음 접했습니다. 문제가있어서 CSV 파일의 데이터를 ARFF로 변환하려고합니다 (Weka에서 제대로 열 수 있음). 요점은 CSV가 손상된 것입니다. , 나는 2 가지 속성 중 하나는 감정이고 하나는 픽셀이지만 사실은 픽셀 하나가 17 개의 다른 픽셀 값에서 만들어 졌다는 것입니다. 필자는 firsts 속성을 List의 ArrayList에 넣을 계획이었는데, 각 목록은 하나의 요소로 구성되며 하나의 감정을 나타냅니다. List의 또 다른 ArrayList는 각 목록이 내 17 픽셀의 묶음인데, 내가 명확한 지 모르지만 여기에는 내 코드가 출력물과 출력물로 표시됩니다. 그래서 여기Java : Integer 목록의 ArrayList를 사용한 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는, 그것이 내 과정에 대한 한을 부여하고 그 이유있어주의하시기 바랍니다 I 이 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에 다시 넣기 전에 List를 지우려고했지만 완전히 비어있는 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); 

당신이 경우 인쇄로 저장 당신은 그것을 얻습니다 :

당신이 다른 갖고 싶어

, 당신은 당신이 처리하는 모든 루프/라인에 대한 귀하의 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

대단히 고마워, 나는 지금 아주 분명해 보인다. 그리고 내가 좀 더 일찍 멍청하다고 느낀다. 감사합니다. :) –