Skip to content

Converting csv to arff

For converting CSV (comma separated value) files into ARFF files you need the following two converters:

In the following you'll find some example code to show you how to use the converters. The class takes 2 arguments:

  • the input CSV file
  • the output ARFF file

Example code:

import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;

import java.io.File;

public class CSV2Arff {
  /**
   * takes 2 arguments:
   * - CSV input file
   * - ARFF output file
   */
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println("\nUsage: CSV2Arff <input.csv> <output.arff>\n");
      System.exit(1);
    }

    // load CSV
    CSVLoader loader = new CSVLoader();
    loader.setSource(new File(args[0]));
    Instances data = loader.getDataSet();

    // save ARFF
    ArffSaver saver = new ArffSaver();
    saver.setInstances(data);
    saver.setFile(new File(args[1]));
    saver.setDestination(new File(args[1]));
    saver.writeBatch();
  }
}
Note: with versions of Weka later than 3.5.3 the call of saver.setDestination(new File(args[1])); is no longer necessary, it is automatically done in the saver.setFile(new File(args[1])); method.

See also#

The Weka Examples collection dedicates several example classes of loading from and saving to various file formats: