I want convert the .csv file to .arff file

can someone suggest what to do?

my code is:

package csvtoarff;

import java.io.File;

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

public class CsvToArff {

    public static void main(String[] args) {

        try{
        String f1 = "C://Users//user//Desktop//testfold//test.csv";

        String f2 = "C://Users//user//Desktop//testfold//output.arff";


        // load the CSV file (input file)
        CSVLoader loader = new CSVLoader();
        loader.setSource(new File(f1));


        String [] options = new String[1];
        options[0]="-H";
        loader.setOptions(options);


        Instances data = loader.getDataSet();
        System.out.println(data);


        // save as an  ARFF (output file)
        ArffSaver saver = new ArffSaver();

        saver.setInstances(data);

        saver.setFile(new File(f2));
        saver.writeBatch();
        }

        catch(Exception e){

        }

    }

I have attached "weka.jar" file in the referenced library.

when I run above code error is:

---Registering Weka Editors---
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?

Personally, I see no reason why you would need a jdbcDriver for this.
Might be because I'm not familiar with .arff files, but doubt it.

first thing to change:

catch(Exception e){
        }

print the stacktrace, don't hide any exceptions thrown.

secondly, find the JDBC.idbDriver, and add it to your classpath

stacktrace is:
java.lang.IllegalArgumentException: Attribute names are not unique! Causes: '50'

my .csv file (which is generated after export operation on mysql DB) is:

25 50 75 50 good
25 25 25 25 good
50 25 25 50 bad

i resolved above error. Now, I wanted to know when exporting .csv file from mysql database how to get column name too(now I am getting only data from my table).

now successfully solved all problems.

solution is:
I used metadata interface to fetch column names.

@peter budo, Thanks for reply. your solution also works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.