Importing Data in Java
Anthony Markham
VP Quant Developer
read().csv()import tech.tablesaw.api.Table;
// Read in data
Table dataTable = Table.read().csv("data.csv");
Table object 💡CsvReadOptions for more controlimport tech.tablesaw.io.csv.CsvReadOptions;
CsvReadOptions options = CsvReadOptions.builder("data.csv").separator(';') // Use semicolon as delimiter.header(true) // First row contains headers.missingValueIndicator("N/A") // Treat "N/A" as missing data.build();// Load the table using the custom options Table table = Table.read().csv(options);
write().csv() for basic CSV exportTable class// Write dataTable to output.csv
dataTable.write().csv("output.csv");
CsvWriteOptions to specify options for writingCsvWriteOptions writeOptions = CsvWriteOptions .builder("output.csv").header(true) // Include column headers.separator(';') // Use semicolon delimiter.quoteAlways(true) // Quote all fields.lineEnd("\r\n") // Windows-style line endings.build();
// Write the CSV using the custom options
Table.write().csv(writeOptions);
// Read CSV, modify, and write back
Table students = Table.read().csv("students.csv");
// View structure
System.out.println(students.structure());
// Save as new file
students.write().csv("students_processed.csv");
Importing Data in Java