ASL
|
This class allows reading/writing CSV files and writing ARFF files.
Files have an optional header with column names, and data rows that can contain numbers (integer or floating point) or strings. When reading, the class tries to infer if there is a header and what separator and decimal symbol are used.
A data row is written when it has as many elements as the header indicates. Make sure you match written data to specified columns!
Reading the file would be done as follows. This class will try to detect the separator used in the read file (if ',', ';' or 'tab'), and will try to detect if there is a header line (when no numbers are found in it).
Or just as:
If a file uses a specific format that autodetection can't handle, use readAs()
to sepecify column types. That includes the ability to read numbers as hexadecimal. For example:
will interpret a row like 20,20,20
as hexadecimal 0x20 (32), string "20" and number 20.0.
For writing ARFF files, just set a file name with a .arff
extension and specify columns with optional types (default is numeric). For nominal types, write classes separated with |, for strings use 's':
#include <TabularDataFile.h>
Public Member Functions | |
TabularDataFile (const String &filename) | |
Creates a data file with name filename for reading or writing. | |
TabularDataFile (const String &filename, const Array< String > &cols) | |
Creates a data file with name filename for writing with the given column names. | |
TabularDataFile & | columns (const String &cols) |
Defines the columns for the data with a comma separated list of column names. | |
TabularDataFile & | columns (const Array< String > &cols) |
Defines the columns for the data. | |
int | numColumns () const |
Returns the number of columns. | |
const Array< String > & | columns () |
Returns the column names. | |
void | setSeparator (char s) |
Set field separator (',' by default) | |
void | setDecimal (char d) |
Set decimal separator (dot by default) | |
void | useQuotes () |
Makes string values be quoted when writing. | |
void | flushEvery (int nrows) |
Forces a real disk write every nrows number of rows. | |
void | readAs (const String &types) |
Sets the types to read for each column as a string with chars: i:int, n:number, h:hex, s:string. | |
TabularDataFile & | operator<< (const Var &x) |
Appends one data item to the current row, and writes the row if it reaches the number of columns defined. | |
Array< Array< Var > > | data () |
Returns the whole file contents as a matrix (array of arrays) | |
bool | nextRow () |
Reads the next row and returns true if it succeded. | |
Var | operator[] (int i) const |
Returns the value for column index i in the current row. | |
Var | operator[] (const String &col) const |
Returns the value for column named col in the current row. | |