Use pd.read_csv("file.txt", sep="\t") (or sep="|" / sep=";" for any custom delimiter, sep=r"\s+" for variable whitespace, or sep=None, engine="python" to auto-detect common delimiters) to parse delimited plain-text files into a Pandas DataFrame—there is no difference between .csv and .txt to pd.read_csv(), which handles any character-separated text file.

Basic Example

import pandas as pd

# 1. Tab-delimited text file (.tsv or .txt):
df_tsv = pd.read_csv("records.txt", sep="\t")

# 2. Pipe-delimited or semicolon-delimited text file with explicit UTF-8 encoding:
df_pipe = pd.read_csv(
    "export.txt",
    sep="|",
    encoding="utf-8",
    na_values=["NA", "NULL", ""],  # Treat custom tokens as missing values
)

# 3. Variable-width whitespace-delimited log/data file (spaces or tabs):
df_ws = pd.read_csv("metrics.txt", sep=r"\s+")

# 4. Headerless delimited file (assign column names manually):
df_no_header = pd.read_csv(
    "raw_values.txt",
    sep=",",
    header=None,
    names=["id", "timestamp", "value"],
)

Key Details & Edge Cases

  • pd.read_table() vs. pd.read_csv():
    • pd.read_table("file.txt") is simply an alias for pd.read_csv("file.txt", sep="\t"). Using pd.read_csv(..., sep=...) explicitly is preferred across modern Pandas codebases because the separator is immediately visible at the call site.
  • Auto-detecting unknown delimiters (sep=None, engine="python"):
    • Passing sep=None triggers Python’s csv.Sniffer via the Python parser engine to infer ,, \t, ;, or | automatically—convenient for ad-hoc exploration, though noticeably slower than the C/PyArrow engines (engine="c" or engine="pyarrow") on large files.
  • Preserving leading zeros in IDs, phone numbers, or ZIP codes (dtype=):
    • By default, pd.read_csv() infers "00412" as integer 412, stripping leading zeros. Pass dtype={"zip_code": "string", "user_id": "string"} (or dtype="string" to read every column as raw text first) so leading zeros are preserved.
  • Handling malformed rows, comment lines, and BOM encodings:
    • on_bad_lines="warn" (or "skip"): Prevents ParserError: Expected X fields in line Y, saw Z from crashing ingestion on noisy log files.
    • comment="#": Ignores metadata or comment lines starting with #.
    • encoding="utf-8-sig": Automatically strips the UTF-8 Byte Order Mark (\ufeff) exported by Windows/Excel tools so the first column name isn’t corrupted into "\ufeffid".
  • Reading huge multi-gigabyte text files without running out of RAM (chunksize= or engine="pyarrow"):
    • Pass usecols=["col1", "col2"] to load only required columns, engine="pyarrow" for multi-threaded parsing of single-character delimiters, or chunksize=100_000 to iterate over batches of rows as an iterator of DataFrames.

Comparison of pd.read_csv() Delimiter & Parsing Options

Delimiter Scenario Recommended Parameters Parser Engine Used Best Used When
Tab-separated (.txt / .tsv) pd.read_csv(path, sep="\t") C / PyArrow (fast) Loading tab-delimited exports from databases, Excel, or bioinformatics tools.
Custom single character (|, ;, :) pd.read_csv(path, sep="|") C / PyArrow (fast) Loading European CSVs (;) or pipe-delimited (|) data dumps.
Variable whitespace (spaces/tabs) pd.read_csv(path, sep=r"\s+") Python regex engine Parsing space-aligned CLI outputs, scientific tables, or server log files.
Unknown delimiter (auto-sniff) pd.read_csv(path, sep=None, engine="python") Python (csv.Sniffer) Quickly inspecting third-party .txt files when the separator isn’t known upfront.
Fixed-width columns (no delimiter) pd.read_fwf(path, widths=[10, 8, 12]) Python fixed-width reader Legacy mainframe or FORTRAN text reports aligned strictly by character column offsets.