Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A placeholder error message is printed when parsing files with ambiguous options #225

Closed
adamroyjones opened this issue Nov 17, 2021 · 2 comments

Comments

@adamroyjones
Copy link
Contributor

I've noticed that the csv gem produces a placeholder error message when attempting to parse a TSV with ambiguous parsing options.

If one were to parse the string

foo\t\tbar

when directed to strip whitespace (i.e., to treat whitespace as insignificant) and use tabs as field separators (i.e., to treat whitespace as significant), then should one parse it as ["foo\t\tbar"], ["foo", "bar"], or ["foo", "", "bar"]? There's no way to choose a parsing strategy that wouldn't cause a reasonable surprise to someone.

I think an error should be produced that informs the user that this is a problem; at the moment, this doesn't happen. With Ruby 3.0.2 and csv 3.2.1, the file

require "csv"

sep = "\t"
tsv_file = "example.tsv"

File.open(tsv_file, "w") { |f| f.puts("foo#{sep}bar") }
CSV.read(tsv_file, col_sep: sep, strip: true)

produces the error

csv-3.2.1/lib/csv/parser.rb:935:in `parse_quotable_robust': TODO: Meaningful message in line 1. (CSV::MalformedCSVError)

I think this is misleading; the file is not really "malformed".

Do you agree with the above? If so, I'm happy to offer up a merge request around this (e.g. to check the options before beginning the parsing, to make sure that if strip is set to true then col_sep must not be whitespace).

@kou
Copy link
Member

kou commented Nov 17, 2021

Good catch!
Could you open a pull request?

@adamroyjones
Copy link
Contributor Author

Thanks for getting back to me so quickly—I'm glad I'm not going mad and that you agree!

I'll work on it and raise a merge request soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants