Read Quoted CSV Fields in Spark: quote and escape Options
How Spark's CSV quote and escape options decide what ends up in your columns, when to set escape to a double quote, and how to check that quoted fields parsed correctly.
Spark's CSV reader uses two settings to decide where a field begins and ends: the quote character and the escape character. They matter most when a value contains the delimiter, a line break, or a quote mark of its own. If they don't match how the file was written, the result is usually not an error. More often you get shifted columns, stray quote marks or split rows. This review covers what each option changes, which combination fits common file styles, and how to confirm the parsed data still means what the source meant.
Short answer: which settings fit your file
Many spreadsheet tools and RFC 4180-style exporters write an embedded quote as two quote characters. For files like that, keep the quote character as a double quote and also set escape to a double quote. Spark's default escape character is a backslash, and that default suits files that write an embedded quote as a backslash followed by a quote. Choose the setting based on how the producing system escapes quotes, not on what looks tidy in a text editor. If any quoted value contains a line break, add multiLine as well. The cost is that a wrong choice can fail silently, so pair the settings with a validation step. Validation is covered further down.
Some files need no quote handling at all. This is the case when no value contains the delimiter, a line break or a quote character. It is also the case when quote marks in the data are ordinary content rather than field wrappers. For those files you can turn quoting off by setting quote to an empty string. If you control the producing system, you have two other ways to avoid quoting problems. You can pick a delimiter that never occurs in the data, such as a tab. You can also skip delimited text and exchange data in a typed columnar format.
What quote and escape actually change
These options are parser rules. They change the characters that end up in each column. They do not change how text is displayed. When a field is read as quoted, Spark removes the surrounding quote characters from the stored value. It also collapses each escape sequence into the single character it stands for. An escape character in this sense has nothing to do with reversing or mirroring text. It is simply a marker that tells the parser the next quote is data, not the end of the field.
- quote: the single character that wraps a field so it can contain the delimiter. The default is a double quote. For reading, an empty string turns quoting off.
- escape: the single character that marks a quote inside a quoted field as literal. The default is a backslash.
- charToEscapeQuoteEscaping: the character that escapes the escape character itself. It applies when escape and quote are different characters.
- multiLine: lets one record span several lines, which quoted fields with embedded line breaks require. The default is false.
- unescapedQuoteHandling: tells the parser what to do when it finds a quote that isn't properly escaped inside a quoted value.
- sep (also accepted as delimiter): the field separator. The default is a comma.
Doubled quotes versus backslash escapes
Below is a small file written in the doubled-quote style. The second field of each data row contains embedded quotes. The second row also contains a comma inside the quoted value.
id,comment
1,"She said ""hello"""
2,"Size: 10"" x 12"", boxed"
The source intends these values: row 1's comment is She said "hello", and row 2's comment is Size: 10" x 12", boxed. Spark is documented to treat a doubled quote as one literal quote when escape is set to a double quote. With the default backslash escape, the doubled quotes are not recognized as escapes. What happens then depends on the unescapedQuoteHandling setting. Possible results include stray quote characters left in the value or the comma in row 2 being treated as a separator. The read below is configured for the doubled-quote style.
df = (spark.read
.option("header", "true")
.option("quote", "\"")
.option("escape", "\"")
.option("multiLine", "true")
.csv(path))
Setting quote to a double quote repeats the default. It is included here only to make the intent explicit. You only need multiLine if some quoted values contain line breaks. Without it, a line break inside a quoted field is read as the end of a record, and the remainder of that value becomes a separate malformed row. Turning multiLine on has a cost. Each file is generally read as a whole rather than split across tasks, which can reduce parallelism on very large single files.
Edge cases worth checking
- Trailing backslashes: with the default backslash escape, a quoted value that ends in a backslash, such as a Windows folder path, can be read as an escaped quote. The field then fails to close where intended. Setting escape to a double quote for doubled-quote files avoids this. charToEscapeQuoteEscaping covers backslash-style files that need a literal escape character.
- Stray quotes in unquoted fields: a value like 5" pipe, with no surrounding quotes, is a common source of misparsing. Review the unescapedQuoteHandling option. RAISE_ERROR is the strictest choice when you want problems surfaced.
- Literal quote content: if quote marks are never used as field wrappers in your data, setting quote to an empty string stops them being removed. Any delimiter inside a value will then split the field.
- Delimiter collisions: if you change sep, for example to a semicolon or a tab, the quote and escape rules still apply. A value only needs quoting if it contains the new delimiter.
Will the parsed data stay usable downstream?
Once a field is parsed, its value is an ordinary string with no wrapping quotes and no escape characters. Displaying, searching or editing it inside Spark works like any other string. Problems come back when you write CSV again. The writer also defaults to a backslash escape, so a value containing a quote is written with a backslash before it. Spreadsheet tools and other RFC 4180-style readers may not interpret that the way you expect. If the output is meant for such tools, set escape to a double quote on the writer as well. The writer also has related options: escapeQuotes, which is on by default, controls whether values containing quotes are wrapped in quotes, and quoteAll wraps every value. Whether a particular destination application reads the output correctly should be confirmed with a test file in that application. One successful import does not prove compatibility everywhere.
Because misconfigured quoting tends to fail quietly, validate the parse rather than trusting it. The mode option controls how malformed rows are handled:
• FAILFAST stops at the first malformed row, which suits strict pipelines.
• PERMISSIVE, the default, keeps malformed rows and sets the fields it can't parse to null. If you supply columnNameOfCorruptRecord with a matching string column in your schema, it also stores the raw text of each failed row in that column.
• DROPMALFORMED discards malformed rows without reporting them, so it can hide exactly the problem you are trying to catch.
A few simple checks catch most mistakes. Compare row counts against the source. Confirm the column count matches the header. Look for leftover quote or backslash characters at the start or end of values. Inspect a sample of rows known to contain commas, quotes or line breaks.
Comparing the configurations
| Configuration | Fits | Main tradeoff |
| Defaults (quote is a double quote, escape is a backslash) | Files that write embedded quotes as a backslash followed by a quote | Doubled-quote files can misparse; a trailing backslash before a closing quote can break a field |
| escape set to a double quote | Spreadsheet and RFC 4180-style exports that double embedded quotes | Backslash-escaped files would no longer have their escapes recognized |
| quote set to an empty string | Data where quote marks are content and values never contain the delimiter | Any delimiter or line break inside a value splits the field |
| multiLine enabled (added to either escape style) | Quoted fields that contain line breaks | Each file is generally read as a whole, which can reduce parallelism on very large files |
The table summarizes documented option behavior. It is not a side-by-side test. No outputs were run against the same input for this article, so differences in edge-case handling between Spark versions are untested. Check the CSV data source documentation for the Spark version you run, especially for unescapedQuoteHandling and charToEscapeQuoteEscaping.
Gaps in this review
This review has no measurements behind it. It contains no performance figures for multiLine reads and no version-by-version test results. It does not confirm how specific spreadsheet or database tools import Spark-written CSV. The examples show expected behavior based on the documented option semantics. Before you rely on a configuration in production, run it against a sample of your real files that includes their messiest rows.