Exporting String data to .csv


#1

Hi Guys,

Does anyone have anything handy lying around to export some String data to a .csv file ?

I’ve got a TableListBox which is basically a three column table acting as a very basic confusion matrix/result-plot for a classification algorithm. Showing the actual class/label of an instance in a test data set vs the class/label predicted by the classifier when fed with the test instance.

Column 1 is just displays and index/count from 1 - N where N is the number of table rows or instances in the test set.

Column 2 shows the “actual” class corresponding to the class/label of the test instance.

Column 3 shows the “predicted” class corresponding to the class/label predicted by the classifier for the instance.

The values in the cells for the “actual” and “predicted columns” are just Strings.

Like so:

Instance    Actual    Predicted
1           Kick      Kick
2           Kick      Snare

Does anyone have an example of exporting this out to a .csv ?

Probably ought to sit and work it out but If the fine JUCE folk have any snippets lying around…well, times a wastin.

Cheers.

Dudes.


#2

Looks like you want this:
http://en.cppreference.com/w/cpp/io/ios_base/setf

Hope that helps


#3

Use OutputStream::writeText(). Just iterate through each item in the Table – the best methodology is to have an OwnedArray of objects which are listed in your Table… then when you need to export them simply have an export method which iterates through the OwnedArray and have a method in the object class which returns a comma separated String

OwnedArray<CMyTableItem> m_Array;

then:

File fOut = szYourFilePath;

if (fOut.existsAsFile())
    fOut.deleteFile();

FileOutputStream fos (fOut);

const bool bAsUTF16 = false;

for (int k = 0; k < m_Array.size(); ++k)
    {
    const CMyTableItem* pItem = m_Array.getUnchecked (k);

    String szCommaSepString = pItem->getCsvString();

    fos.writeText (szCommaSepString.toUTF8(), bAsUTF16, false);
    }

fos.flush();

This isn’t real code – just something I wrote off the top of my head… but something along these lines should work.

Rail


#4

Cheers guys.

I was wondering whether I was missing some obvious helper in the API docs!

Like the suggestions. Shouldn’t be too fiddly to add in.

Thans for the semi-pseudo code @Rail_Jon_Rogut ! Iterating through the table items and whacking out a delimited string makes sense.

Josh


#5

Just a heads-up that that pseudo-code is probably overcomplicated and would actually do the wrong thing depending on the arguments you pass to writeText().

You only need something as simple as

File f = getYourFile();
f.deleteFile();

for (auto thing : yourThings)
    f << thing.item1 << "\t" << thing.item2 << "\t" << thing.item3 << newLine;

#6

Cheers Jules.

Will have a quick go at that later and post my solution in case it’s handy for others exporting out from table contents or anything.

Josh