Package groovy.csv
Class CsvSlurper
java.lang.Object
groovy.csv.CsvSlurper
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 -
Method Summary
Modifier and TypeMethodDescriptionParse CSV from a file.parse(InputStream stream) Parse CSV from an input stream.Parse CSV from a reader.Parse CSV from a path.<T> List<T>Parse CSV from a file into typed objects.<T> List<T>Parse CSV from a reader into typed objects.<T> List<T>Parse CSV into typed objects using Jackson databinding.<T> List<T>Parse CSV from a path into typed objects.Parse the content of the specified CSV text.setQuoteChar(char quoteChar) Set the quote character (default: double-quote).setSeparator(char separator) Set the column separator character (default: comma).setUseHeader(boolean useHeader) Set whether the first row is a header row (default: true).
-
Constructor Details
-
CsvSlurper
public CsvSlurper()
-
-
Method Details
-
setSeparator
Set the column separator character (default: comma).- Parameters:
separator- the separator character- Returns:
- this slurper for chaining
-
setQuoteChar
Set the quote character (default: double-quote).- Parameters:
quoteChar- the quote character- Returns:
- this slurper for chaining
-
setUseHeader
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
-
parseText
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
Parse CSV from a reader. WhenuseHeaderis true (the default), each row is returned as a map keyed by column headers from the first row. WhenuseHeaderis false, maps are keyed by auto-generated column names.- Parameters:
reader- the reader of CSV- Returns:
- a list of maps (one per row)
-
parse
Parse CSV from an 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
Parse CSV from a file.- Parameters:
file- the CSV file- Returns:
- a list of maps (one per row), keyed by column headers
- Throws:
IOException
-
parse
Parse CSV from a path.- Parameters:
path- the path to the CSV file- Returns:
- a list of maps (one per row), keyed by column headers
- Throws:
IOException
-
parseAs
Parse CSV into typed objects using Jackson databinding. Supports@JsonPropertyand@JsonFormatannotations for column mapping and type conversion.- Type Parameters:
T- the target type- Parameters:
type- the target typecsv- the CSV text- Returns:
- a list of typed objects
-
parseAs
Parse CSV from a reader into typed objects.- Type Parameters:
T- the target type- Parameters:
type- the target typereader- the reader of CSV- Returns:
- a list of typed objects
-
parseAs
Parse CSV from a file into typed objects.- Type Parameters:
T- the target type- Parameters:
type- the target typefile- the CSV file- Returns:
- a list of typed objects
- Throws:
IOException
-
parseAs
Parse CSV from a path into typed objects.- Type Parameters:
T- the target type- Parameters:
type- the target typepath- the path to the CSV file- Returns:
- a list of typed objects
- Throws:
IOException
-