CSV::MalformedCSVError
RubyERRORNotableParsing
CSV input is not valid
Quick Answer
Rescue CSV::MalformedCSVError and log the offending line; consider using liberal_parsing: true for lenient mode.
What this means
Raised by the CSV library when input cannot be parsed according to CSV rules — typically due to unclosed quotes, illegal quoting, or characters that violate the configured delimiter/quote settings.
Why it happens
- 1Unclosed double-quote in a field
- 2Mismatched or illegal use of the quote character
- 3File encoding mismatch causing multi-byte sequences to look like stray quotes
Fix
Enable liberal_parsing for tolerant mode
Enable liberal_parsing for tolerant mode
require 'csv' CSV.parse(data, liberal_parsing: true) do |row| process(row) end
Why this works
liberal_parsing relaxes strict RFC 4180 compliance, accepting many common real-world deviations.
Code examples
Reproducing the errorruby
require 'csv'
CSV.parse('a,"b,c
d,e')
# CSV::MalformedCSVError: Unclosed quoted field on line 1.Liberal parsingruby
CSV.parse(messy_data, liberal_parsing: true) { |row| puts row }Rescue per-lineruby
File.foreach('data.csv').with_index(1) do |line, num|
begin
CSV.parse_line(line)
rescue CSV::MalformedCSVError => e
warn "Line #{num} skipped: #{e.message}"
end
endSame error in other languages
Sources
Official documentation ↗
Ruby Standard Library Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev