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

Improved column separator detection by ignoring quoted sections #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/smarter_csv/auto_detection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ def guess_column_separator(filehandle, options)
count.times do
line = readline_with_counts(filehandle, options)
delimiters.each do |d|
candidates[d] += line.scan(d).count
escaped_quote = Regexp.escape(options[:quote_char])

# Count only non-quoted occurrences of the delimiter
non_quoted_text = line.split(/#{escaped_quote}[^#{escaped_quote}]*#{escaped_quote}/).join

candidates[d] += non_quoted_text.scan(d).count
end
rescue EOFError # short files
break
Expand Down
18 changes: 18 additions & 0 deletions spec/features/formating/column_separator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
end.to raise_exception SmarterCSV::NoColSepDetected
end

it 'does not detect separators that are between quotes' do
data = SmarterCSV.process("#{fixture_path}/separator_chars_between_quotes.csv", options)


expect(data.first.keys.size).to eq 5
expect(data.size).to eq 3
end

context 'when auto is given as a string' do
let(:options) do
{
Expand Down Expand Up @@ -148,6 +156,16 @@
end.to raise_exception SmarterCSV::NoColSepDetected
end

it 'does not detect separators that are between quotes' do
data = SmarterCSV.process(
"#{fixture_path}/separator_chars_between_quotes_no_headers.csv",
options.merge(user_provided_headers: %w[Name Age Job Department Project])
)

expect(data.first.keys.size).to eq 5
expect(data.size).to eq 3
end

context 'when auto is given as a string' do
let(:options) do
{
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/separator_chars_between_quotes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"name, info":"age, years":"job, title":"department, info":"project, code"
"John, Doe":"35, years":"Senior, Developer":"Engineering, Dept":"Code, 1234"
"Jane, Smith":"29, years":"Project, Manager":"Product, Development":"Code,5678"
"Emily, Jones":"42, years":"CTO,":"Technology,Dept":"Code,9012"
3 changes: 3 additions & 0 deletions spec/fixtures/separator_chars_between_quotes_no_headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"John, Doe":"35, years":"Senior, Developer":"Engineering, Dept":"Code, 1234"
"Jane, Smith":"29, years":"Project, Manager":"Product, Development":"Code,5678"
"Emily, Jones":"42, years":"CTO,":"Technology,Dept":"Code,9012"