使用 Tablesaw 处理 CSV

Java 中的数据导入

Anthony Markham

VP Quant Developer

读取 CSV 文件

  • 使用 read().csv() 读取 CSV 文件
import tech.tablesaw.api.Table;
// Read in data
Table dataTable = Table.read().csv("data.csv");
  • 自动检测列类型,生成结构化 Table 对象 💡
Java 中的数据导入

CSV 读取选项

  • 使用 CsvReadOptions 获得更精细的控制
import 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);
Java 中的数据导入

写入 CSV 文件

  • 使用 write().csv() 进行基本导出
  • Table 类提供
  • 保留列类型与结构
  • 自动处理特殊字符
// Write dataTable to output.csv
dataTable.write().csv("output.csv");
Java 中的数据导入

CSV 写入选项

  • 使用 CsvWriteOptions 指定写入选项
CsvWriteOptions 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);
Java 中的数据导入

CSV 工作流

  • 完整流程:读取 -> 检查 -> 处理 -> 写入
  • 非破坏性操作(创建新文件) 📁
// 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");
Java 中的数据导入

开始练习!

Java 中的数据导入

Preparing Video For Download...