Class Csv
For example:
import static com.google.common.labs.csv.Csv.CSV;
// skip(1) to skip the header row.
List<List<String>> rows = CSV.parseToLists(input).skip(1).toList();
You can also use the header row, and parse each row to a Map keyed by the header
field names:
import static com.google.common.labs.csv.Csv.CSV;
List<Map<String, String>> rows = CSV.parseToMaps(input).toList();
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionparseToLists(Reader csv) Parses theinputreader into a lazy stream of immutableList, one row at a time.parseToLists(String csv) Parses theinputstring into a lazy stream of immutableList, one row at a time.parseToMaps(Reader csv) Similar toparseToMaps(String), but takes aReaderinstead.parseToMaps(String csv) Parsescsvstring lazily, returning each row in aMapkeyed by the field names in the header row.<R> Stream<R> parseWithHeaderFields(Reader csv, BiCollector<? super String, ? super String, ? extends R> rowCollector) CSV.parseWithHeaderFields(input, toMap())will parse the non-header rows from theinputreader into a lazy stream ofMap, keyed by the header field names.<R> Stream<R> parseWithHeaderFields(String csv, BiCollector<? super String, ? super String, ? extends R> rowCollector) CSV.parseWithHeaderFields(input, toMap())will parse the non-header rows in theinputstring into a lazy stream ofMap, keyed by the header field names.toString()Returns an otherwise equivalent CSV parser but allows comment rows.withDelimiter(char delimiter) Returns an otherwise equivalent CSV parser but usingdelimiterinstead of comma.
-
Field Details
-
CSV
Default CSV parser. Configurable usingwithComments()andwithDelimiter(char).
-
-
Method Details
-
withDelimiter
Returns an otherwise equivalent CSV parser but usingdelimiterinstead of comma. -
withComments
Returns an otherwise equivalent CSV parser but allows comment rows.Comments are recognized by the presence of a hash sign (#) at the beginning of a line.
Note that comments are not standard CSV specification.
-
parseToLists
Parses theinputstring into a lazy stream of immutableList, one row at a time.No special treatment of the header row. If you know you have a header row, consider calling
.skip(1)to skip it, or useparseToMaps(java.lang.String)with the field names as the Map keys. -
parseToLists
Parses theinputreader into a lazy stream of immutableList, one row at a time.No special treatment of the header row. If you know you have a header row, consider calling
.skip(1)to skip it, or useparseToMaps(java.lang.String)with the field names as the Map keys.Implementation note: the parser uses internal buffer so you don't need to wrap it in
BufferedReader. -
parseToMaps
Parsescsvstring lazily, returning each row in aMapkeyed by the field names in the header row. The first non-empty row is expected to be the header row.Upon duplicate header names, the latter wins. If you need alternative strategies, such as to reject duplicate header names, or to use
ListMultimapto keep track of all duplicate header values, consider usingparseWithHeaderFields(String, BiCollector)instead. That is:
or:import static com.google.mu.util.stream.BiCollectors.toMap; CSV.parse(input, toMap()); // throw upon duplicate header namesimport static com.google.common.collect.ImmutableListMultimap; // keep track of duplicate header names CSV.parse(input, ImmutableListMultimap::toImmutableListMultimap); -
parseToMaps
-
parseWithHeaderFields
public <R> Stream<R> parseWithHeaderFields(String csv, BiCollector<? super String, ? super String, ? extends R> rowCollector) CSV.parseWithHeaderFields(input, toMap())will parse the non-header rows in theinputstring into a lazy stream ofMap, keyed by the header field names.Usually, if you need a
Mapof field names to column values, consider usingparseToMaps(String)instead. But if you need alternative strategies, such as collecting each row to aListMultimapto more gracefully handle duplicate header names, you can use:import static com.google.common.collect.ImmutableListMultimap; CSV.parseWithHeaderFields(input, ImmutableListMultimap::toImmutableListMultimap); -
parseWithHeaderFields
public <R> Stream<R> parseWithHeaderFields(Reader csv, BiCollector<? super String, ? super String, ? extends R> rowCollector) CSV.parseWithHeaderFields(input, toMap())will parse the non-header rows from theinputreader into a lazy stream ofMap, keyed by the header field names.import static com.google.common.collect.ImmutableListMultimap; CSV.parseWithHeaderFields(input, ImmutableListMultimap::toImmutableListMultimap);Implementation note: the parser uses internal buffer so you don't need to wrap it in
BufferedReader. -
toString
-