Reference library

Files & data

Read and write files safely; parse JSON, CSV, and common text formats.

2 matches
Files & data easy

How to Read a TSV File in Python with csv.DictReader

Read a tab-separated (TSV) file into dictionaries using the csv module's DictReader with a tab delimiter.

csv tsv file-io
Python
import csv
from pathlib import Path

data_file = Path("data.tsv")

# Sample TSV content (tab-separated)
sample = """name\tage\tcity
Alice\t30\tNew York
Bob\t25\tLos Angeles
Carol\t35\tChicago
"""
data_file.write_text(sample)

with data_file.open("r", newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f, d…
14 0 Open
Files & data easy

Parse CSV with Custom Delimiter and Quote Character in Python

Reads a CSV string with a custom delimiter and quote character using the csv module, returning a list of rows.

csv parsing delimiter
Python
import csv
from io import StringIO

def parse_csv(data, delimiter='|', quotechar='"'):
    reader = csv.reader(StringIO(data), delimiter=delimiter, quotechar=quotechar)
    rows = [row for row in reader]
    return rows

if __name__ == "__main__":
    sample = 'Alice|"Smith, Jr."|25\nBob|"Johnson, Sr."|30'
    result …
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Files & data — Python code examples

What you will find here

This page collects files & data snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.