📊CSV Import Templates
CSV Quoting & Escaping Rules
Comprehensive guide to CSV field quoting, escaping, and special character handling.
Explanation
Understanding when and how to quote CSV fields is crucial for correct parsing.
Examples
Quoting Examples
Output
name,description,tags Simple Name,No special chars,tag1 "Name, With Comma",Contains comma in middle,"tag1,tag2" "Quote ""Example""",Has internal ""quotes"",tag3 "Line Break","Multi line value",tag4
Code Examples
JavaScript
// Proper CSV field escaping
function escapeCSVField(field) {
const str = String(field);
// Quote if contains comma, quote, or newline
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
// Double internal quotes
return '"' + str.replace(/"/g, '""') + '"';
}
return str;
}
// Generate CSV row
function toCSVRow(fields) {
return fields.map(escapeCSVField).join(',');
}
// Example usage
const row = toCSVRow([
'John Doe',
'Description with, comma',
'Has "quotes" inside'
]);
// Result: John Doe,"Description with, comma","Has ""quotes"" inside"Try it Now
💡 Tips
- Quote fields containing: comma, quote, newline
- Double internal quotes (not backslash)
- Don't quote unless necessary
- Be consistent with line endings (\n vs \r\n)
- Trailing commas indicate empty fields
⚠️ Common Pitfalls
- Backslash escaping is NOT standard CSV
- Excel may handle quotes differently
- Some parsers don't support multiline fields
- Missing closing quote breaks entire file
- Different libraries have different quirks