csvkit

2.2.0 · active · verified Wed Apr 15

csvkit is a suite of powerful command-line tools for working with CSV files. It enables users to convert, clean, analyze, and process tabular data efficiently from the command line. The current version is 2.2.0. Its release cadence is moderate, with major versions often representing significant architectural changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use csvkit's command-line tools (`csvlook` and `csvsql`) programmatically from Python via the `subprocess` module. It creates a sample CSV, pretty-prints it, and then queries it.

import subprocess
import os

# Create a dummy CSV file
csv_content = "name,age\nAlice,30\nBob,24\nCharlie,35"
file_path = "example.csv"
with open(file_path, "w") as f:
    f.write(csv_content)

print("Original CSV content:")
print(csv_content)

# Use csvlook to pretty-print (run as a subprocess)
print("\n--- Output from csvlook ---")
try:
    result = subprocess.run(['csvlook', file_path], capture_output=True, text=True, check=True)
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Error running csvlook: {e.stderr}")

# Use csvsql to query data (run as a subprocess)
print("\n--- Output from csvsql (names of people over 25) ---")
try:
    result = subprocess.run(
        ['csvsql', '--query', 'SELECT name FROM example WHERE age > 25', file_path],
        capture_output=True, text=True, check=True
    )
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Error running csvsql: {e.stderr}")

# Clean up the dummy file
os.remove(file_path)
print(f"\nCleaned up {file_path}")

view raw JSON →