When exporting customer records from a CRM, downloading bank transaction statements, or transferring tabular data into an SQL database, the universal format of choice is CSV (Comma-Separated Values).

Despite the rise of sophisticated binary formats and relational databases, the humble CSV remains the world’s most widely used data interchange standard. What is a CSV file, how do its parsing rules work under RFC 4180, and why does opening a CSV in spreadsheet applications sometimes mangle text formatting?

The Anatomy of a CSV File

At its core, a CSV file is a pure plain text file that represents two-dimensional tabular data (rows and columns). It contains no visual styles, no cell colors, no font declarations, and no mathematical formulas.

Every line in the file represents a single table row (record), and within that row, individual fields (columns) are separated by a delimiter—most commonly a comma:

ID,ProductName,Quantity,UnitPrice,Status
101,Standard Widget,50,14.99,Shipped
102,"Heavy-Duty Bolt, Stainless",1200,0.85,In Stock
103,Precision Gear,15,45.50,Backordered

Because it is plain text, a CSV can be opened and parsed by any programming language (Python, C++, R, JavaScript), any operating system shell, and any text editor from Windows Notepad to Vim.

The RFC 4180 Standard: Formal Grammar Rules

For decades, CSV was implemented informally with subtle variations between different software vendors. In October 2005, the Internet Engineering Task Force (IETF) published RFC 4180 to establish official grammar rules:

Rule 1: Record Separation (Line Breaks)

Each record must be located on a separate line, delimited by a line break:

  • Formally, RFC 4180 mandates CRLF (\r\n - Carriage Return followed by Line Feed).
  • Modern parsers also accept standard Unix LF (\n).

Rule 2: Optional Header Line

The very first record in the file may contain column header names corresponding to the fields in the document.

Rule 3: Handling Commas Inside Data Fields (Quoting)

What happens when a data value contains a literal comma—such as a street address "123 Main St, Suite 400" or a product name "Heavy-Duty Bolt, Stainless"?

  • If a comma were left unquoted, a parser would split the address into two separate columns, shifting all subsequent table cells out of alignment.
  • Under RFC 4180, any field containing a comma must be enclosed within double quotation marks ("...").

Rule 4: Escaping Literal Quotation Marks

If a data field itself contains a double quote (for example, a screen dimension like 27" Monitor), RFC 4180 requires escaping the quote by doubling it:

"27"" 4K Monitor",1,450.00

When parsed, the software interprets "" as a single literal quotation mark " within the cell text.

Common CSV Pitfalls and Troubleshooting

1. The Regional Delimiter Problem (Comma vs. Semicolon)

In English-speaking countries (US, UK, Canada, Australia), a period (.) denotes a decimal point (e.g., 19.99), and a comma is used as the field separator.

However, in many European countries (Germany, France, Spain, Italy), a comma is the official mathematical decimal mark (e.g., 19,99). If a software program attempted to use a comma as both the column separator and the decimal separator, numbers would become unparseable.

  • Consequently, European editions of Microsoft Excel default to using a semicolon (;) as the CSV delimiter.
  • If you open an American comma-delimited CSV in a European locale, Excel may cram the entire row into a single column. (Fix: Use Excel’s Data > Text to Columns feature).

2. The Excel “Leading Zero” Erasure

If your CSV contains postal codes (such as 01234) or credit card numbers, Excel often attempts to “help” by guessing that the column is numeric:

  • It strips the leading zero, transforming the ZIP code 01234 into the number 1234.
  • To prevent this, data generators must force text interpretation by formatting the field as ="01234" or opening the file via Excel’s Power Query / Import Data wizard rather than double-clicking.

3. Character Encoding (UTF-8 with BOM)

If a CSV contains accented European characters (é, ü), Chinese kanji, or Cyrillic text, saving it in raw UTF-8 can cause older editions of Excel to display corrupted characters (mojibake). Adding a UTF-8 Byte Order Mark (BOM) at the start of the file signals Excel to decode characters properly.

CSV vs. XLSX: When to Use Which?

Feature CSV XLSX
File Type Plain text Compressed ZIP archive with XML
Compatibility 100% universal (Any script, DB, or OS) Requires spreadsheet-aware software
File Size for Raw Data Very compact Slightly larger due to XML overhead
Calculations / Formulas None (Static values only) Dynamic formulas and functions
Multiple Worksheets Single table only Multiple sheets and workbooks
Data Types Everything is raw text until parsed Explicit types (Date, Float, Bool)