CSV (Comma-Separated Values) is a simple file format used to store and exchange tabular data. It is widely used in data analysis, spreadsheets, databases, and data exchange between applications.
Each line in a CSV file represents a row, and values are separated by commas.
Name, Age, Email
John Doe, 30, johndoe@example.com
Alice Smith, 25, alicesmith@example.com
Other delimiters like semicolons or tabs can be used:
Name; Age; Email
John Doe; 30; johndoe@example.com
Name Age Email
John Doe 30 johndoe@example.com
import csv
with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
import csv
data = [
["Name", "Age", "Email"],
["John Doe", 30, "johndoe@example.com"],
["Alice Smith", 25, "alicesmith@example.com"]
]
with open("output.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(data)
let csvText = "Name,Age,Email\nJohn Doe,30,johndoe@example.com";
let rows = csvText.split("\n").map(row => row.split(","));
console.log(rows);
Feature | CSV | JSON | XML |
---|---|---|---|
Readability | Simple, human-readable | Readable, but structured | Verbose, hierarchical |
Structure | Flat (rows/columns) | Key-value pairs | Tree-based |
Values containing commas must be enclosed in quotes (`"`).
Name, Age, Address
"John Doe", 30, "123 Main St, New York"
Use quotes around values that contain line breaks.
Name, Description
"Product 1", "This is a great product.
It has multiple features."
Use empty fields for missing values.
Name, Age, Email
John Doe, 30, johndoe@example.com
Alice Smith, , alicesmith@example.com
CSV is a simple and effective format for storing tabular data. It is widely supported across applications and is easy to process programmatically.