CSV Quotes Adder
Add or fix quotes in CSV fields. Supports RFC 4180 quoting, custom delimiters, and multiple quoting modes. Paste, upload, or type CSV data.
About
Malformed CSV files cause silent data corruption. A single unquoted comma inside an address field shifts every subsequent column, breaking database imports, ETL pipelines, and spreadsheet formulas. The RFC 4180 specification mandates that fields containing the delimiter character, double-quote ("), or a line break must be enclosed in double-quotes, with internal quotes escaped by doubling (""). This tool parses your CSV character-by-character using a finite state machine and re-serializes it with correct quoting applied. It does not use regular expressions for parsing, which fail on edge cases involving embedded newlines.
Four quoting modes are supported. "All Fields" wraps every field unconditionally. "Only When Needed" applies minimal quoting per RFC 4180 rules. "Force Numeric" and "Force Text" target fields by detected type. The tool auto-detects your delimiter from comma, semicolon, tab, and pipe by frequency analysis on the first 5 lines. Limitation: this tool assumes UTF-8 encoding and does not handle binary content or BOM markers.
Formulas
The quoting decision for each field F follows a boolean predicate based on the selected mode:
needsQuote(F, mode) =
The escape function for internal quotes uses the doubling rule:
Where delim is the active delimiter character (comma, semicolon, tab, or pipe), quote is the double-quote character U+0022, and newline matches both \n (LF) and \r\n (CRLF). The function isNumeric tests if the trimmed field matches the pattern /^-?\d+(\.\d+)?$/ (optional negative, digits, optional decimal).
Reference Data
| Scenario | Raw Field Value | Correctly Quoted Output | RFC 4180 Rule |
|---|---|---|---|
| Field contains delimiter | New York, NY | "New York, NY" | Section 2.6 |
| Field contains double-quote | He said "hello" | "He said ""hello""" | Section 2.7 |
| Field contains newline | Line1\nLine2 | "Line1\nLine2" | Section 2.6 |
| Field with leading/trailing spaces | βdataβ | "βdataβ" | Recommended practice |
| Empty field | (empty) | "" | Optional per mode |
| Numeric field (integer) | 12345 | 12345 | No quoting needed |
| Numeric field forced quoted | 00123 | "00123" | Preserves leading zeros |
| Field already correctly quoted | "valid" | "valid" | No change needed |
| Field with escaped quotes inside | "She said ""hi""" | "She said ""hi""" | Idempotent operation |
| Boolean-like field | TRUE | TRUE | No quoting needed |
| Date field | 2024-01-15 | 2024-01-15 | No special chars |
| URL field | https://example.com/path?q=1&b=2 | https://example.com/path?q=1&b=2 | No delimiter present |
| Tab-delimited with comma | Price, USD (tab sep) | Price, USD | Comma is not the delimiter |
| Pipe-delimited with pipe in data | A|B (pipe sep) | "A|B" | Field contains delimiter |
| Semicolon-delimited EU CSV | 1.234,56 (semicolon sep) | 1.234,56 | Comma is not the delimiter |