Écrire des fichiers CSV formatés
Après avoir nettoyé vos données, vous devez les exporter pour différents destinataires. L’équipe analytics souhaite des CSV standard, les systèmes européens attendent des séparateurs par point-virgule, et un mainframe hérité exige des fichiers sans en-tête. Des options d’export flexibles garantissent une intégration fluide de vos données dans des environnements techniques variés.
Les classes Table et CsvWriteOptions ont été importées pour vous.
Cet exercice fait partie du cours
Importer des données en Java
Instructions
- Écrivez la table au format CSV standard.
- Écrivez avec un séparateur point-virgule.
- Écrivez sans en-têtes pour un système hérité.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
public class WriteCSVFiles {
public static void main(String[] args) {
try {
Table employeePerformance = Table.read().csv("employee_performance.csv");
System.out.println("Original data structure:");
System.out.println(employeePerformance.structure());
System.out.println("First 3 rows:");
System.out.println(employeePerformance.first(3));
// Write the table as a standard CSV
employeePerformance.____.csv("employee_performance_standard.csv");
// Write with a semicolon separator
employeePerformance.write().csv(CsvWriteOptions
.____("employee_performance_semicolon.csv")
.____(';')
.build());
// Write without headers for a legacy system
employeePerformance.write().csv(CsvWriteOptions
.builder("employee_performance_noheader.csv")
.____(____)
.build());
System.out.println("\nFiles successfully written to output directory.");
Table readBack = Table.read().csv("employee_performance_standard.csv");
System.out.println("\nVerification - Reading back semicolon-separated file:");
System.out.println(readBack.first(3));
} catch (Exception e) {
System.err.println("Error processing CSV files: " + e.getMessage());
}
}
}