Initializing playground…
← Back to roadmap

📐 readFwf — Interactive Playground

Parse fixed-width formatted text into a DataFrame with readFwf(). Mirrors pandas read_fwf() — column boundaries are inferred from whitespace patterns automatically, or provided explicitly via colspecs / widths.
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Auto column-width inference

When colspecs is omitted (default "infer"), readFwf() scans the data rows and identifies separator positions — character columns where every row contains a space. This mirrors pandas.read_fwf(colspecs='infer').

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 · Explicit colspecs

Provide colspecs — an array of [start, end) character index pairs — for precise control over column boundaries. Useful when separator positions vary between rows.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 · Column widths

Alternatively, pass widths — an array of integers — to define consecutive column widths. This produces [0,w0], [w0,w0+w1], … colspecs internally.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 · Missing values & dtype forcing

Standard NA strings (NA, NaN, null, …) are recognised automatically. Add custom NA strings with naValues. Force a column's dtype with the dtype option.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 · Index column, row limits & skip rows

Promote a column to the row index with indexCol. Limit rows with nRows and skip leading data rows with skipRows.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 · Real-world: Census-style population table

Fixed-width format is common in government datasets, legacy mainframe exports, and statistical software output. Here is a Census-style table.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

API Reference

Parse a fixed-width formatted text string into a DataFrame. Equivalent to pandas.read_fwf().

readFwf(text: string, options?: ReadFwfOptions): DataFrame

type ColSpec = readonly [number, number];   // [start, end) character indices

interface ReadFwfOptions {
  colspecs?:   readonly ColSpec[] | "infer"; // column boundaries (default: "infer")
  widths?:     readonly number[];            // column widths (alternative to colspecs)
  inferNrows?: number;                       // rows to sample for inference (default: 100)
  header?:     number | null;               // header row index (default: 0)
  names?:      readonly string[];           // explicit column names
  indexCol?:   string | number | null;      // column to use as row index
  dtype?:      Record<string, DtypeName>; // force dtype for named columns
  naValues?:   readonly string[];           // extra NA string values
  skipRows?:   number;                      // data rows to skip after header
  nRows?:      number;                      // maximum data rows to read
}