简体   繁体   English

Java:使用List of Integer的ArrayList读取CSV

[英]Java : CSV reading with ArrayList of List of Integer

So, hello all, this is my first question here. 所以,大家好,这是我的第一个问题。 Indeed after looking for a while I didn't find anything helpful. 确实在找了一段时间后我没有找到任何有用的东西。 I am quite new to java and Data Mining. 我是java和数据挖掘的新手。 I have an issue, so I am trying to get some data from a CSV file to convert it to an ARFF (which will allow me to open it properly in Weka) So the point is that the CSV I have is kind of broken, I have 2 attributes one is emotions and one is pixels, but the fact is that the pixels one is made from 17 different pixel value. 我有一个问题,所以我试图从CSV文件中获取一些数据以将其转换为ARFF(这将允许我在Weka中正确打开它)所以重点是我所拥有的CSV有点破碎,我有两个属性,一个是情感,一个是像素,但事实是像素一个是由17个不同的像素值组成。 I was planning to put my firsts attribute in an ArrayList of List where each list is made of one element and represent one emotions. 我计划将我的第一个属性放在List的ArrayList中,其中每个列表由一个元素组成并代表一种情绪。 And another ArrayList of List where each list is a bunch of my 17 pixels I don't know if I am clear, but here is my code followed by my output and by what I want as output. 另一个列表的ArrayList,其中每个列表是我17个像素的一堆,我不知道我是否清楚,但这里是我的代码,后面是我的输出和我想要的输出。

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);
    }}

So here is a shorten CSV I am using : 所以这是我正在使用的缩短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 

(Please note that the CSV as I told you is kind of broken, it's the given one for my coursework and that's why I am trying to fix it first) (请注意,我告诉你的CSV有点坏了,这是给我的课程作业,这就是为什么我要先修复它)

Finally by running this code for this CSV I have this as output : 最后,通过为此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]]

While I want this : 虽然我想要这个:

[[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]]

I hope I am clear. 我希望我很清楚。 I know that it's just because I am using the add method to a list, but I didn't manage to complete this task even by using the scanner reading line by line, or some other methods. 我知道这只是因为我正在使用add方法列表,但是我甚至无法通过逐行使用扫描仪读取或其他方法来完成此任务。 And I also tried to clear the List before putting it again in the ArrayList but it juste give an totally EMPTY ArrayList. 我还尝试在将它再次放入ArrayList之前清除List,但是juste给出了一个完全EMPTY ArrayList。

Thank you very much for your help. 非常感谢您的帮助。

you have a list of lists 你有一份清单清单

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

and you read your file into a list 并将您的文件读入列表

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

and store it into the List of Lists 并将其存储在列表列表中

Emotions.add(emotions);

if you print that out you get: 如果你打印出来,你得到:

[[0, 1, 3]]

if you want to have it different, you have to create your List<Integer> emotions for every loop/line you process, so eventually your fillArrayList method may look like this: 如果你想让它与众不同,你必须为你处理的每个循环/行创建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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM