Sobes.tech
Intern

What is CSV?

sobes.tech AI

Answer from AI

Text format for representing tabular data, where columns are separated by a specific character (usually a comma), and rows are separated by newline characters. It is used for data exchange between different applications.

Main characteristics:

  • Simplicity: CSV files are easy to create and read by both machines and humans.
  • Prevalence: Supported by most spreadsheet processors, databases, and data analysis tools.
  • Delimiters: Most commonly a comma, but other characters (semicolon, tab) can also be used.
  • Headers: May contain a header row with column names.
  • Escaping: Values containing delimiters or newline characters are usually enclosed in quotes, and quotes within values are doubled or escaped.

Working with CSV in Python is usually done using the built-in csv module:

import csv

# Reading from CSV
with open('data.csv', 'r', newline='', encoding='utf-8') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row)

# Writing to CSV
data = [['Name', 'Age'], ['Alice', 30], ['Bob', 25]]
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(data)