Package groovy.csv

# Class CsvSlurper

java.lang.Object
groovy.csv.CsvSlurper

@Incubating public class CsvSlurper extends Object
Represents a CSV parser.

Usage:

     def csv = new groovy.csv.CsvSlurper().parseText('name,age\nAlice,30\nBob,25')
     assert csv[0].name == 'Alice'
     assert csv[1].age == '25'

Since:
  6.0.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CsvSlurper()`
  Creates a slurper that uses comma-separated columns, double quotes, and treats the first row as headers.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `List<Map<String,String>>`
  `parse(File file)`
  Parse CSV from a UTF-8 encoded file.
  `List<Map<String,String>>`
  `parse(File file, Charset charset)`
  Parse CSV from a file decoded with the given charset.
  `List<Map<String,String>>`
  `parse(InputStream stream)`
  Parse CSV from a UTF-8 encoded input stream.
  `List<Map<String,String>>`
  `parse(InputStream stream, Charset charset)`
  Parse CSV from an input stream decoded with the given charset.
  `List<Map<String,String>>`
  `parse(Reader reader)`
  Parse CSV from a reader.
  `List<Map<String,String>>`
  `parse(Path path)`
  Parse CSV from a UTF-8 encoded path.
  `List<Map<String,String>>`
  `parse(Path path, Charset charset)`
  Parse CSV from a path decoded with the given charset.
  `<T> List<T>`
  `parseAs(Class<T> type, File file)`
  Parse CSV from a UTF-8 encoded file into typed objects.
  `<T> List<T>`
  `parseAs(Class<T> type, File file, Charset charset)`
  Parse CSV from a file decoded with the given charset into typed objects.
  `<T> List<T>`
  `parseAs(Class<T> type, Reader reader)`
  Parse CSV from a reader into typed objects.
  `<T> List<T>`
  `parseAs(Class<T> type, String csv)`
  Parse CSV into typed objects using Jackson databinding.
  `<T> List<T>`
  `parseAs(Class<T> type, Path path)`
  Parse CSV from a UTF-8 encoded path into typed objects.
  `<T> List<T>`
  `parseAs(Class<T> type, Path path, Charset charset)`
  Parse CSV from a path decoded with the given charset into typed objects.
  `List<Map<String,String>>`
  `parseText(String csv)`
  Parse the content of the specified CSV text.
  `CsvSlurper`
  `setColumns(String... columns)`
  Set explicit column names, typically for CSV without a header row.
  `CsvSlurper`
  `setColumns(List<String> columns)`
  Set explicit column names, typically for CSV without a header row.
  `CsvSlurper`
  `setQuoteChar(char quoteChar)`
  Set the quote character (default: double-quote).
  `CsvSlurper`
  `setSeparator(char separator)`
  Set the column separator character (default: comma).
  `CsvSlurper`
  `setUseHeader(boolean useHeader)`
  Set whether the first row is a header row (default: true).

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### CsvSlurper

    public CsvSlurper()
    Creates a slurper that uses comma-separated columns, double quotes, and treats the first row as headers.

- ## Method Details

  - ### setSeparator

    public CsvSlurper setSeparator(char separator)
    Set the column separator character (default: comma).
    Parameters:
      `separator` - the separator character
    Returns:
      this slurper for chaining

  - ### setQuoteChar

    public CsvSlurper setQuoteChar(char quoteChar)
    Set the quote character (default: double-quote).
    Parameters:
      `quoteChar` - the quote character
    Returns:
      this slurper for chaining

  - ### setUseHeader

    public CsvSlurper setUseHeader(boolean useHeader)
    Set whether the first row is a header row (default: true).
    Parameters:
      `useHeader` - true to treat the first row as headers
    Returns:
      this slurper for chaining

  - ### setColumns

    public CsvSlurper setColumns(String... columns)
    Set explicit column names, typically for CSV without a header row. When column names are supplied and `useHeader` is true, the first row is still consumed as a header row but the supplied names take precedence over the names it contains.
    Parameters:
      `columns` - the column names
    Returns:
      this slurper for chaining

  - ### setColumns

    public CsvSlurper setColumns(List<String> columns)
    Set explicit column names, typically for CSV without a header row. When column names are supplied and `useHeader` is true, the first row is still consumed as a header row but the supplied names take precedence over the names it contains.
    Parameters:
      `columns` - the column names
    Returns:
      this slurper for chaining

  - ### parseText

    public List<Map<String,String>> parseText(String csv)
    Parse the content of the specified CSV text.
    Parameters:
      `csv` - the CSV text
    Returns:
      a list of maps (one per row), keyed by column headers

  - ### parse

    public List<Map<String,String>> parse(Reader reader)
    Parse CSV from a reader. When explicit column names have been supplied via `setColumns(String...)`, each row is returned as a map keyed by those names. Otherwise, when `useHeader` is true (the default), maps are keyed by column headers from the first row. Otherwise, maps are keyed by generated column names `c1`, `c2`, ...
    Parameters:
      `reader` - the reader of CSV
    Returns:
      a list of maps (one per row)

  - ### parse

    public List<Map<String,String>> parse(InputStream stream)
    Parse CSV from a UTF-8 encoded input stream. The caller is responsible for closing the stream.
    Parameters:
      `stream` - the input stream of CSV
    Returns:
      a list of maps (one per row)

  - ### parse

    public List<Map<String,String>> parse(InputStream stream, Charset charset)
    Parse CSV from an input stream decoded with the given charset. The caller is responsible for closing the stream.
    Parameters:
      `stream` - the input stream of CSV
      `charset` - the charset used to decode the stream
    Returns:
      a list of maps (one per row)

  - ### parse

    public List<Map<String,String>> parse(File file) throws IOException
    Parse CSV from a UTF-8 encoded file.
    Parameters:
      `file` - the CSV file
    Returns:
      a list of maps (one per row), keyed by column headers
    Throws:
      `IOException`

  - ### parse

    public List<Map<String,String>> parse(File file, Charset charset) throws IOException
    Parse CSV from a file decoded with the given charset.
    Parameters:
      `file` - the CSV file
      `charset` - the charset used to decode the file
    Returns:
      a list of maps (one per row), keyed by column headers
    Throws:
      `IOException`

  - ### parse

    public List<Map<String,String>> parse(Path path) throws IOException
    Parse CSV from a UTF-8 encoded path.
    Parameters:
      `path` - the path to the CSV file
    Returns:
      a list of maps (one per row), keyed by column headers
    Throws:
      `IOException`

  - ### parse

    public List<Map<String,String>> parse(Path path, Charset charset) throws IOException
    Parse CSV from a path decoded with the given charset.
    Parameters:
      `path` - the path to the CSV file
      `charset` - the charset used to decode the file
    Returns:
      a list of maps (one per row), keyed by column headers
    Throws:
      `IOException`

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, String csv)
    Parse CSV into typed objects using Jackson databinding. Supports `@JsonProperty` and `@JsonFormat` annotations for column mapping and type conversion.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `csv` - the CSV text
    Returns:
      a list of typed objects

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, Reader reader)
    Parse CSV from a reader into typed objects. When explicit column names have been supplied via `setColumns(String...)`, values are bound to properties of those names by position. Otherwise, when `useHeader` is true (the default), values are bound by the column names in the header row. Otherwise, positional columns are derived from the target type's properties: component declaration order for records, alphabetical order for other classes. Use `@JsonPropertyOrder` on the type to set an explicit column order.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `reader` - the reader of CSV
    Returns:
      a list of typed objects

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, File file) throws IOException
    Parse CSV from a UTF-8 encoded file into typed objects.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `file` - the CSV file
    Returns:
      a list of typed objects
    Throws:
      `IOException`

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, File file, Charset charset) throws IOException
    Parse CSV from a file decoded with the given charset into typed objects.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `file` - the CSV file
      `charset` - the charset used to decode the file
    Returns:
      a list of typed objects
    Throws:
      `IOException`

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, Path path) throws IOException
    Parse CSV from a UTF-8 encoded path into typed objects.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `path` - the path to the CSV file
    Returns:
      a list of typed objects
    Throws:
      `IOException`

  - ### parseAs

    public <T> List<T> parseAs(Class<T> type, Path path, Charset charset) throws IOException
    Parse CSV from a path decoded with the given charset into typed objects.
    Type Parameters:
      `T` - the target type
    Parameters:
      `type` - the target type
      `path` - the path to the CSV file
      `charset` - the charset used to decode the file
    Returns:
      a list of typed objects
    Throws:
      `IOException`