CsvParser
CsvParser is a CSV parser that focuses on identifying errors.
Installation
Add this line to your application's Gemfile:
gem 'csv_parser'
And then execute:
$ bundle
Or install it yourself as:
$ gem install csv_parser
Usage
CsvParser's goal is to give you descriptive error messages.
Error types
- missing closing quote (
CsvParser::MissingQuoteError
) - quote in the wrong place (
CsvParser::StrayQuoteError
) - rows with not enough fields (
CsvParser::MissingFieldsError
) - rows with too many fields (
CsvParser::ExtraFieldsError
)
Options
You can pass in an options hash to the CsvParser.parse
method
that contains one or more of the following options:
-
:field_sep
- specify field separator (default is","
) -
:record_sep
- specify record separator (default is"\n"
) -
:quote_char
- specify quote character (default is"\""
) -
:allow_empty_record
- specify whether empty records are allowed (default istrue
) -
:skip_empty_record
- specify whether empty records are skipped (default istrue
) -
:allow_uneven_records
- specify whether records with different field lengths are allowed (default istrue
)
Example
require 'csv_parser'
data = <<EOF
foo,"bar
baz,quz
EOF
begin
result = CsvParser.parse(data)
rescue CsvParser::Error => e
# e is a CsvParser::MissingQuoteError
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request