开始使用免费开始使用

读取 CSV 文件

您刚加入 DataCorp,担任初级数据分析师。您的第一个项目是整合员工数据库,但数据来自多个系统,CSV 格式各不相同——有的是逗号分隔,有的是制表符分隔。能够处理多种文件格式非常关键,因为真实世界中的数据很少采用单一的标准格式。

TableCsvReadOptions 以及相关的 Tablesaw 类已为您导入。

本练习是课程的一部分

Java 中的数据导入

查看课程

练习说明

  • 使用默认选项读取 "employees.csv"
  • 使用制表符分隔符读取 "employees_tab.csv"
  • 打印两个表的结构。

交互式实操练习

通过完成这段示例代码来试试这个练习。

public class ReadCSVFiles {
    public static void main(String[] args) {
        try {
            // Read file using default options
            Table standardCsv = Table.____().____("employees.csv");
            
            // Read file using a tab delimiter
            Table tabDelimited = Table.read().csv(CsvReadOptions
                .____("employees_tab.csv")
                .____('\t')
                .build());
            
            // Print the structure of both tables
            System.out.println(____.____());
            System.out.println(____.____());
            
        } catch (Exception e) {
            System.err.println("Error reading CSV files: " + e.getMessage());
        }
    }
}
编辑并运行代码