safenames

June 11, 2026 · View on GitHub

Modify headers of a CSV to only have "safe" names - guaranteed "database-ready"/"CKAN-ready" names.

Table of Contents | Source: src/cmd/safenames.rs | CKAN

Description | Usage | Safenames Options | Common Options

Description

Modify headers of a CSV to only have "safe" names - guaranteed "database-ready" names (optimized specifically for PostgreSQL column identifiers).

Fold to lowercase. Trim leading & trailing whitespaces. Replace whitespace/non-alphanumeric characters with _. If name starts with a number & check_first_char is true, prepend the unsafe prefix. If a header with the same name already exists, append a sequence suffix (e.g. col, col_2, col_3). Names are limited to 60 bytes in length (snapped to UTF-8 char boundary, including any duplicate-disambiguation suffix). Empty names are replaced with the unsafe prefix.

In addition, specifically because of CKAN Datastore requirements:

  • Headers with leading underscores are replaced with "unsafe_" prefix.
  • Headers that are named "_id" are renamed to "reserved__id".

These CKAN Datastore options can be configured via the --prefix & --reserved options, respectively.

In Always (a) and Conditional (c) mode, returns number of modified headers to stderr, and sends CSV with safe headers output to stdout.

In Verify (v) mode, returns number of unsafe headers to stderr. In Verbose (V) mode, returns number of headers; duplicate count and unsafe & safe headers to stderr. No stdout output is generated in Verify and Verbose mode.

In JSON (j) mode, returns Verbose mode info in minified JSON to stdout. In Pretty JSON (J) mode, returns Verbose mode info in pretty printed JSON to stdout.

Given data.csv:
c1,12_col,Col with Embedded Spaces,,Column!@Invalid+Chars,c1 1,a2,a3,a4,a5,a6

$ qsv safenames data.csv

c1,unsafe_12_col,col_with_embedded_spaces,unsafe_,column__invalid_chars,c1_2 1,a2,a3,a4,a5,a6 stderr: 5

Conditionally rename headers, allowing "quoted identifiers":

$ qsv safenames --mode c data.csv

c1,unsafe_12_col,Col with Embedded Spaces,unsafe_,column__invalid_chars,c1_2 1,a2,a3,a4,a5,a6 stderr: 4

Verify how many "unsafe" headers are found:

$ qsv safenames --mode v data.csv

stderr: 4

Verbose mode:

$ qsv safenames --mode V data.csv

stderr: 6 header/s 1 duplicate/s: "c1:2" 4 unsafe header/s: ["12_col", "Col with Embedded Spaces", "", "Column!@Invalid+Chars"] 1 safe header/s: ["c1"]

"Safer" (s) mode - like always, but collapses runs of non-alphanumeric characters into a single _ (note "column_invalid_chars", not "column__invalid_chars"):

$ qsv safenames --mode s data.csv

c1,unsafe_12_col,col_with_embedded_spaces,unsafe_,column_invalid_chars,c1_2 1,a2,a3,a4,a5,a6 stderr: 5

"Safer" with unicode (S) mode - same as s, but preserves unicode letters & numbers. Given a header "Café #5", "--mode S" yields "café_5", whereas the ASCII "--mode s" strips the accent, yielding "caf_5":

$ qsv safenames --mode S data.csv

The --collapse & --unicode flags can be combined with ANY mode, including the verify & JSON modes, so the report reflects the "safer" rewrite:

$ qsv safenames --mode j --collapse --unicode data.csv

Note that even if "Col with Embedded Spaces" is technically safe, it is generally discouraged. Though it can be created as a "quoted identifier" in PostgreSQL, it is still marked "unsafe" by default, unless mode is set to "conditional."

It is discouraged because the embedded spaces can cause problems later on. (see https://lerner.co.il/2013/11/30/quoting-postgresql/ for more info).

For more examples, see https://github.com/dathere/qsv/blob/master/tests/test_safenames.rs. See also https://github.com/dathere/qsv/wiki/Transform-and-Reshape#safenames

Usage

qsv safenames [options] [<input>]
qsv safenames --help

Safenames Options

     Option     TypeDescriptionDefault
 ‑‑mode stringRename header names to "safe" names — guaranteed "database-ready" names. Mode is selected by the FIRST character: c/C conditional, a/A always, s safer, S Safer (unicode), v verify, V Verbose, j JSON, J pretty JSON (case matters for s vs S, v vs V and j vs J; --mode verbose maps to 'v', NOT V).Always
 ‑‑reserved stringComma-delimited list of additional case-insensitive reserved names that should be considered "unsafe." If a header name is found in the reserved list, it will be prefixed with "reserved_"._id
 ‑‑prefix stringCertain systems do not allow header names to start with "" (e.g. CKAN Datastore). This option allows the specification of the unsafe prefix to use when a header starts with "".unsafe_
 ‑‑collapse flagCollapse consecutive runs of non-alphanumeric characters into a single _. Composes with ALL modes (including verify & JSON modes). Implied by --mode s and --mode S.
 ‑‑unicode flagPreserve unicode letters & numbers instead of stripping to ASCII. Composes with ALL modes (including verify & JSON modes). Implied by --mode S.

Common Options

     Option     TypeDescriptionDefault
 ‑h,
‑‑help 
flagDisplay this message
 ‑o,
‑‑output 
stringWrite output to instead of stdout. Note that no output is generated for Verify and Verbose modes.
 ‑d,
‑‑delimiter 
stringThe field delimiter for reading CSV data. Must be a single character. (default: ,)

Source: src/cmd/safenames.rs | Table of Contents | README