Package smile.io

Class CSV

java.lang.Object
smile.io.CSV

public class CSV extends Object
Reads and writes files in variations of the Comma Separated Value (CSV) format.
  • Constructor Details

    • CSV

      public CSV()
      Constructor. Standard Comma Separated Value format, as for RFC4180 but allowing empty lines.
    • CSV

      public CSV(org.apache.commons.csv.CSVFormat format)
      Constructor.
      Parameters:
      format - the CSV file format.
  • Method Details

    • schema

      public CSV schema(StructType schema)
      Sets the schema.
      Parameters:
      schema - the data schema.
      Returns:
      this object.
    • charset

      public CSV charset(Charset charset)
      Sets the charset.
      Parameters:
      charset - the charset of file.
      Returns:
      this object.
    • read

      public DataFrame read(String path) throws IOException, URISyntaxException
      Reads a CSV file.
      Parameters:
      path - the input file path.
      Returns:
      the data frame.
      Throws:
      IOException - when fails to read the file.
      URISyntaxException - when the file path syntax is wrong.
    • read

      public DataFrame read(String path, int limit) throws IOException, URISyntaxException
      Reads a limited number of records from a CSV file.
      Parameters:
      path - the input file path.
      limit - the number of records to read.
      Returns:
      the data frame.
      Throws:
      IOException - when fails to read the file.
      URISyntaxException - when the file path syntax is wrong.
    • read

      public DataFrame read(Path path) throws IOException
      Reads a CSV file.
      Parameters:
      path - the input file path.
      Returns:
      the data frame.
      Throws:
      IOException - when fails to read the file.
    • read

      public DataFrame read(Path path, int limit) throws IOException
      Reads a limited number of records from a CSV file.
      Parameters:
      path - the input file path.
      limit - the number of records to read.
      Returns:
      the data frame.
      Throws:
      IOException - when fails to read the file.
    • inferSchema

      public StructType inferSchema(Reader reader, int limit) throws IOException
      Infer the schema from the top n rows.
      1. Infer type of each row.
      2. Merge row types to find common type
      3. String type by default.
      Parameters:
      reader - the file reader.
      limit - the number of records to read.
      Returns:
      the data frame.
      Throws:
      IOException - when fails to read the file.
    • write

      public void write(DataFrame data, Path path) throws IOException
      Writes the data frame to a csv file with UTF-8 encoding.
      Parameters:
      data - the data frame.
      path - the output file path.
      Throws:
      IOException - when fails to write the file.