Write CSV Without Quotes in Spark
How to write CSV without quotes in Spark, what changes in the file, when unquoted output stays parseable, and how to check your data before you switch quoting off.
Spark's CSV writer adds quotation marks only in certain cases, but those quotes often surprise people: a downstream system expects bare values, empty strings come out as two quote characters, or embedded quotes are escaped with backslashes. This review explains what removing quotes changes in the file, which writer options are involved, and when unquoted output is safe to use. It relies on Spark's documented CSV options and general CSV parsing rules. No version-specific tests were supplied, so verify the behavior on your own Spark version.
Is unquoted CSV suitable for your use?
Use unquoted output only when you can show that no value contains the delimiter, a line break, or the quote character, or when a receiving system explicitly requires bare values and you control the data. The benefit is a simpler file that matches strict import formats. The cost is that quoting is the only thing that lets a parser tell a delimiter inside a value from a delimiter between values. Once quotes are gone, one stray comma or newline shifts every following column in that row, and nothing in the file reveals that this happened. If your data includes free text, such as names, addresses, comments or descriptions, keep standard quoting and change the receiving side instead.
- Reasonable fits: numeric or code-like columns, IDs, dates, fixed enumerations, and data already validated to exclude delimiters and line breaks.
- Poor fits: user-entered text, values copied from documents, multi-line fields, and any column whose contents you cannot constrain.
- Standard quoted CSV is the better choice: when the file will be read by general-purpose CSV tools, or when you need a lossless round trip back into Spark.
What "without quotes" can mean
The phrase covers several different goals, and each one maps to a different option. Decide which one you need before changing settings.
- No quotes around empty strings: Spark writes empty strings as two quote characters by default, so they stay distinct from nulls, which are written as nothing.
- No quotes around values that contain a quote character: by default, Spark encloses such values in quotes.
- Different escaping of embedded quotes: by default, Spark escapes an inner quote with a backslash instead of doubling it.
- No quoting at all: every value is written as is, whatever it contains.
What the change actually does to the file
This is a change to the stored characters, not to how the file is displayed. Removing quotes deletes the bytes that mark where a field starts and ends. A parser has no other information to rebuild those boundaries. Take a row with the values Smith, John and 42. Quoted, it is written as "Smith, John",42 and reads back as two fields. Unquoted, it becomes Smith, John,42, which any comma-delimited reader will split into three fields: Smith, a value with a leading space, and 42. The meaning of the row has changed, not just its look.
Embedded quotes show the escaping difference. A value of He said "hi" is written by default as "He said "hi"" because the documented default escape character is a backslash. Many tools that follow the common CSV convention expect doubled quotes instead: "He said ""hi""". Setting the escape option to a double quote produces that form. If your real problem is a reader rejecting backslash escapes, this one setting may fix it without removing quotes at all.
Writer options that control quoting
| Option | Documented write default | Effect |
| quote | double quote | The quote character. The documentation says an empty string on write makes Spark use the null character (U+0000). |
| escape | backslash | The character used to escape quotes inside a quoted value. |
| escapeQuotes | true | Encloses values that contain the quote character in quotes. |
| quoteAll | false | Quotes every value. This is the opposite of what this article covers. |
| emptyValue | two quote characters | How empty strings are written. |
| nullValue | empty string | How nulls are written. |
| ignoreLeadingWhiteSpace / ignoreTrailingWhiteSpace | true for writing | Trims whitespace from values on write. |
The whitespace defaults matter for preserving meaning. Spark's documentation lists trimming as the write default, so values with intentional leading or trailing spaces can change no matter how you set quoting. Set both options to false if spacing matters.
Option-based approach
These option names are the same in the Scala, Java and Python writer APIs. The example below uses PySpark. Keep only the lines that match your goal: for example, if you only want empty strings left bare, set emptyValue alone.
(df.write
.option("header", True)
.option("quote", "")
.option("escapeQuotes", False)
.option("emptyValue", "")
.option("ignoreLeadingWhiteSpace", False)
.option("ignoreTrailingWhiteSpace", False)
.mode("overwrite")
.csv(output_path))
Two caveats apply. First, because an empty quote setting is documented to fall back to the null character, check whether that character shows up in the output for values that would otherwise have been quoted. That outcome depends on your data and Spark version, and this review has no test result to confirm it either way. Second, after emptyValue is set to an empty string, nulls and empty strings look identical in the file. Choose a distinct nullValue if the receiving system needs to tell them apart.
Writing raw lines with the text writer
If you need exact control with no quoting logic at all, build each line yourself and write it with the text writer. Nothing is quoted or escaped, so what you build is exactly what gets stored.
from pyspark.sql import functions as F
cols = [F.coalesce(F.col(c).cast("string"), F.lit("")) for c in df.columns]
(df.select(F.concat_ws(",", *cols).alias("value"))
.write.mode("overwrite")
.text(output_path))
The coalesce step matters. concat_ws skips null inputs instead of leaving an empty slot, so without it a null would remove a delimiter and shift the remaining columns left. The text writer also has no header option. To add a header row, you would combine it with the data yourself, and line order across multiple output files is not guaranteed unless you write a single partition.
Check the data before removing quotes
Since unquoted output is only safe when values contain no delimiters, line breaks or quote characters, test that condition directly instead of assuming it. The pattern below assumes a comma delimiter; adjust it if you use a different separator.
from functools import reduce
from pyspark.sql import functions as F
risky = reduce(lambda a, b: a | b,
[F.col(c).cast("string").rlike(r'[,\r\n"]') for c in df.columns])
bad_rows = df.filter(risky).count()
If bad_rows is greater than zero, unquoted output will damage those rows. As a second check, read the written files back with the same delimiter and compare the row count and column count with the source DataFrame. A mismatch confirms that field boundaries were lost.
Will the file stay usable after copying or sharing?
An unquoted file is ordinary plain text, so copying it does not change the characters. What changes is how other programs interpret it. Readers that follow common CSV conventions treat every unquoted delimiter as a field boundary and every unquoted line break as a new record. That is correct for clean data and wrong for anything else. A file that looks fine when opened as text can still load misaligned in a spreadsheet or database import. If the null-character fallback appears in output, some editors and loaders may display or handle that character unpredictably. Concerns like screen-reader reading order do not really apply to a data file. The usability question is whether each receiving system parses it the way you intend, and that has to be checked per destination.
Keep a standard quoted version alongside the unquoted one when the file goes to multiple consumers, when the data may later include free text, or when someone may need to load it back into Spark without loss.
Evidence gaps
- No version-specific test results were available. Option behavior above reflects Spark's documented CSV options and should be confirmed on your version.
- Whether the null-character quote fallback appears in output for your data has not been verified here.
- No specific downstream tool, such as a spreadsheet program or database loader, was tested. Check each destination with a sample file before relying on it.