Why Quotes are Needed in CSV
Quotes are used in CSV files to handle special cases where data contains:
- Commas (the delimiter)
- Line breaks
- Quotes themselves
Basic Examples
1. Simple CSV (No quotes needed)
Id,Name,Salary
1,John,1000
2,Jane,2000
2. CSV with Commas in Data (Quotes needed)
Id,Name,Salary,Department
1,"Smith, John",1000,"IT, Development"
2,"Doe, Jane",2000,"HR, Recruitment"
The quotes tell the CSV parser: "Everything between these quotes is one field, even if it contains commas"
3. CSV with Quotes in Data (Escaped quotes)
Id,Name,Comment
1,John,"He said ""Hello World"""
2,Jane,"She replied ""Nice to meet you"""
Double quotes ""
inside quoted fields represent a single quote character.
Raw Java Parser Logic:
boolean inQuotes = false; // Track if we're inside quoted text
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == '"') {
if (inQuotes && i < line.length() - 1 && line.charAt(i + 1) == '"') {
// This is an escaped quote "" → "
currentValue.append('"');
i++; // Skip the next quote
} else {
// Toggle quote state (start or end of quoted section)
inQuotes = !inQuotes;
}
} else if (c == ',' && !inQuotes) {
// Only treat comma as delimiter if NOT inside quotes
values.add(currentValue.toString());
currentValue = new StringBuilder();
} else {
currentValue.append(c);
}
}
OpenCSV Configuration:
.withCSVParser(new com.opencsv.CSVParserBuilder()
.withSeparator(',') // Field separator
.withQuoteChar('"') // Character that wraps special fields
.withEscapeChar('\\') // Alternative escape character (optional)
.build())
Real-world Examples
Example 1: Address with commas
Without quotes (WRONG):
Name,Address
John,123 Main St,New York,NY
→ Parsed as: "John", "123 Main St", "New York", "NY"
With quotes (CORRECT):
Name,Address
John,"123 Main St, New York, NY"
→ Parsed as: "John", "123 Main St, New York, NY"
Example 2: Text with quotes
Input:
Id,Message
1,"She said ""I love CSV files"""
Parsed as:
- Field 1: "1"
- Field 2: "She said ""I love CSV files""" becomes → "She said "I love CSV files""
Visual Guide
"This is one, field" , "Another field" , RegularField
│ │ │ │ │
│ │ │ │ └─ No quotes needed
│ │ │ └─ Quote character
│ │ └─ Comma inside quotes = part of data
│ └─ Comma outside quotes = field separator
└─ Quote starts a special field
Key Points to Remember:
- Quotes protect special characters inside them from being treated as delimiters
- Double quotes "" inside quoted fields represent a single quote "
- When inQuotes = true, commas are treated as regular text, not separators
- OpenCSV handles this automatically - you just need to configure the quote character
Top comments (0)