Skip to main content

A tool to manipulate csv files with headers.

Project description

csvspoon: a tool to manipulate csv file with headers

Again, again, and again.

Installing

From pypi:

pip3 install csvspoon

Or developer version:

git clone <this repo>
cd csvspoon
pip3 install -e .

Enable completion (for bash or other shells using bash-completion)

mkdir -p ~/.local/share/bash-completion/completions
register-python-argcomplete csvspoon > ~/.local/share/bash-completion/completions/csvspoon

Python module

All methods and functions are accessible in the python module.

Cli usage

usage: csvspoon [-h] {cat,apply,filter,sort,join,aggregate} ...

A tool to manipulate csv files with headers.
Again, again and again.

optional arguments:
  -h, --help            show this help message and exit

subcommands:
  {cat,apply,filter,sort,join,aggregate}
    cat                 Concatenate csv files.
    apply               Apply a formula to compute a new column.
    filter              Filter a csv with a formula.
    sort                Sort csv files.
    join                Operate join on csv files
    aggregate           Apply a aggregation formula to compute a new column.

csvspoon cat

usage: csvspoon cat [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT]
                    [input [input ...]]

Concatenate csv files.
Empty fields added if some columns do not exist in all files
This method is completely streamed and no data is stored in memory.

positional arguments:
  input                 Input file specification. If no input file is
                        provided, stdin is used as first input file, otherwise
                        use explicitly "-" for stdin. Can be a filename (e.g.
                        "file.csv"), a filename followed a semicolon and
                        column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.

Examples:
  Change delimiter of a csv file:
    csvspoon cat -d "\t" -u ";" file.csv > result.csv

  Change delimiter of a csv file with specified output:
    csvspoon cat -o result.csv -d "\t" -u ";" file.csv

  Cat two csv files:
    csvspoon cat file1.csv file2.csv

  Reformat two columns of a csv files:
    csvspoon cat -f a_colname:5.1f -f another_colname:04d file.csv

  Cat one csv file, keeping only a column:
    csvspoon cat file.csv:a_col

  Cat two csv files, renaming a column on the second file:
    csvspoon cat file1.csv file2.csv:new_col=old_col,another_col

csvspoon apply

usage: csvspoon apply [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT]
                      [-b BEFORE] [--np] [--sp] [-t TYPE] [-a COLSPEC FORMULA]
                      [input]

Apply a formula to compute a new column.
The formula must be a valid python expression evaluated on each row.
This method is completely streamed and no data is stored in memory.

positional arguments:
  input                 Input file specification. If no input file is
                        provided, stdin is used as input file. Can be a
                        filename (e.g. "file.csv"), a filename followed a
                        semicolon and column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.
  -b BEFORE, --before BEFORE
                        Run the following code before evaluate the expression
                        on each row. Can be specified multiple times. (e.g.
                        "import math").
  --np                  Shortcut to `--before "import numpy as np"`
  --sp                  Shortcut to `--np --before "import scipy as sp"`
  -t TYPE, --type TYPE  Apply type conversion on specified command prior to
                        expression. The argument must be a column name
                        followed by a valid Python type. See "--before" to
                        define non standard type. e.g. "a_column:int" or
                        "a_column:float". This option can be specified
                        multiple time to type different columns.
  -a COLSPEC FORMULA, --add COLSPEC FORMULA, --add-column COLSPEC FORMULA
                        Append a new column (or update existing one). Take two
                        argument, COLSPEC and FORMULA. COLSPEC is the name of
                        the created column obtained by appling the formula.
                        The column is remplaced if already exists. The COLSPEC
                        can also contains a colon and a format specifier, see
                        "--format" for example. FORMULA must be a valid python
                        expression. For the current row, columns values are
                        accessible as local variable. e.g. "a_colname +
                        other_colname" or "min(a_colname, other_colname)". See
                        "--type" for typing other columns and "--before" for
                        run code before evaluating expression. Can be
                        specified multiple time.

Examples:
  Combine text columns by a formula:
    csvspoon apply -a name "lastname.upper()+' '+firstname.lower()" file.csv

  Sum to integer columns:
    csvspoon apply -t cola:int -t colb:int -a colsum "cola+colb" file.csv

  Sum to integer columns and format the result:
    csvspoon apply -t cola:int -t colb:int -a colsum:05d "cola+colb" file.csv

  Compute complex expression between columns:
    csvspoon apply \
            -b "import math" \
            -t x:float \
            -t y:float \
            -a norm "math.sqrt(x**2+y**2)" \
            file.csv

  Multiple computation can be done reusing newly created columns:
    csvspoon apply -t x:int -a x2p1 "x**2+1" -a x2p1m1 "x2p1-1" file.csv

csvspoon filter

usage: csvspoon filter [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT]
                       [-b BEFORE] [--np] [--sp] [-t TYPE] [-a FILTER_FORMULA]
                       [input]

Evaluate a formula on each row, and keep only rows where the formula
is evaluated True.
The formula must be a valid python expression evaluated on each row.
This method is completely streamed and no data is stored in memory.

positional arguments:
  input                 Input file specification. If no input file is
                        provided, stdin is used as input file. Can be a
                        filename (e.g. "file.csv"), a filename followed a
                        semicolon and column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.
  -b BEFORE, --before BEFORE
                        Run the following code before evaluate the expression
                        on each row. Can be specified multiple times. (e.g.
                        "import math").
  --np                  Shortcut to `--before "import numpy as np"`
  --sp                  Shortcut to `--np --before "import scipy as sp"`
  -t TYPE, --type TYPE  Apply type conversion on specified command prior to
                        expression. The argument must be a column name
                        followed by a valid Python type. See "--before" to
                        define non standard type. e.g. "a_column:int" or
                        "a_column:float". This option can be specified
                        multiple time to type different columns.
  -a FILTER_FORMULA, --add FILTER_FORMULA, --add-filter FILTER_FORMULA
                        FORMULA must be a valid python expression, which is
                        casted to bool(). For the current row, columns values
                        are accessible as local variable. e.g. "a_colname >
                        other_colname" or "a_colname=='fixedvalue'". See "--
                        type" for typing other columns and "--before" for run
                        code before evaluating filter expression. Can be
                        specified multiple time.

Examples:
  Filter csv file using two columns:
    csvspoon filter -a "lastname!=firstname" file.csv

  Chain filters on csv file:
    csvspoon filter \
            -a "lastname.startswith('Doe')" \
            -a "firstname.starswith('John')" \
            file.csv

  Filter csv file with float column price:
    csvspoon filter -t price:float -a "price>12.5" file.csv

  Filter csv file with complex expression:
    csvspoon filter \
            -b "import math" \
            -t x:float \
            -t y:float \
            -t z:float \
            -a "math.sqrt(x**2+y**2)>z" \
            file.csv

csvspoon sort

usage: csvspoon sort [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT]
                     [-k KEYS] [-n] [-r] [-R]
                     [input]

Sort csv file.
Warning: this method need to store in memory all the input csv file.

positional arguments:
  input                 Input file specification. If no input file is
                        provided, stdin is used as input file. Can be a
                        filename (e.g. "file.csv"), a filename followed a
                        semicolon and column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.
  -k KEYS, --key KEYS   Column used for sorting. Can be specified multiple
                        time.
  -n, --numeric-sort    Compare according to numerical value.
  -r, --reverse         Reverse the result of comparisons.
  -R, --random-sort     Shuffle. If key specified, shuffle is performed inside
                        lines with the same key.

Examples:
  Sort csv file using column cola:
    csvspoon sort -k cola file.csv

  Sort csv file using columns cola and colb:
    csvspoon sort -k cola -k colb file.csv

  Sort csv file using numerical mode on column numcol:
    csvspoon sort -n -k numcol file.csv

  Shuffle csv file:
    csvspoon sort -R file.csv

csvspoon join

usage: csvspoon join [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT] [-l]
                     [-r] [-e]
                     input [input ...]

Natural join of csv files.
Joins are performed from left to right.
Warning: this method need to store in memory all csv except the
    first which is streamed.

If neither --left or --right specified, inner join is realized. For
complete outer join, use --left and --right together.

positional arguments:
  input                 Input file specification. If less than two input files
                        are provided, stdin is used as first input file,
                        otherwise use explicitly "-" for stdin. Can be a
                        filename (e.g. "file.csv"), a filename followed a
                        semicolon and column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.
  -l, --left            Perform left join. If more than two files are
                        provided, each join in a left join. Can be used with
                        `-r` to obtain a outer join.
  -r, --right           Perform right join. If more than two files are
                        provided, each join in a right join. Can be used with
                        `-l` to obtain a outer join.
  -e, --empty           Indicate than empty field have to be considered as a
                        value.

Examples:
  Operate NATURAL JOIN on two csv files:
    csvspoon join file1.csv file2.csv

  Operate two NATURAL JOIN on three csv files:
    csvspoon join file1.csv file2.csv file3.csv

  Operate LEFT JOIN on two csv files
    csvspoon join -l file1.csv file2.csv

  Operate RIGHT JOIN on two csv files
    csvspoon join -r file1.csv file2.csv

  Operate OUTER JOIN on two csv files
    csvspoon join -lr file1.csv file2.csv

csvspoon aggregate

usage: csvspoon aggregate [-h] [-d DELIM] [-o OUTPUT] [-u ODELIM] [-f FORMAT]
                          [-b BEFORE] [--np] [--sp] [-t TYPE]
                          [-a COLSPEC FORMULA] [-k KEYS]
                          [input]

Apply a formula to compute a new column.
The formula must be a valid python expression evaluated for each
groupped row.
Only aggregation or column with non ambiguous values are keeped.
Warning: this method need to store in memory all the input csv file.

positional arguments:
  input                 Input file specification. If no input file is
                        provided, stdin is used as input file. Can be a
                        filename (e.g. "file.csv"), a filename followed a
                        semicolon and column names separated by commas (e.g.
                        "file.csv:a_colname,another_colname"). A column can be
                        renamed while reading the file (e.g.
                        "file.csv:a_colname,new_colname=old_colname"). When
                        column names are specified, only these columns are
                        used, with the provided order.

optional arguments:
  -h, --help            show this help message and exit
  -d DELIM, --delim DELIM
                        Input delimiter. (default: ',')
  -o OUTPUT, --output OUTPUT
                        Output file, else output on stdout.
  -u ODELIM, --output-delim ODELIM
                        Output delimiter. (default: ',')
  -f FORMAT, --format FORMAT
                        Apply a format on a column on output. The argument
                        must be a column name followed by a colon and a format
                        specifier. e.g. "a_colname:5d" or "a_colname:+07.2f".
                        This option can be specified multiple time to format
                        different columns.
  -b BEFORE, --before BEFORE
                        Run the following code before evaluate the expression
                        on each row. Can be specified multiple times. (e.g.
                        "import math").
  --np                  Shortcut to `--before "import numpy as np"`
  --sp                  Shortcut to `--np --before "import scipy as sp"`
  -t TYPE, --type TYPE  Apply type conversion on specified command prior to
                        expression. The argument must be a column name
                        followed by a valid Python type. See "--before" to
                        define non standard type. e.g. "a_column:int" or
                        "a_column:float". This option can be specified
                        multiple time to type different columns.
  -a COLSPEC FORMULA, --add COLSPEC FORMULA, --add-aggregation COLSPEC FORMULA
                        Append a new colon by aggregation of values. Take two
                        argument, COLSPEC and FORMULA. COLSPEC is the name of
                        the created column obtained the aggregation. The
                        COLSPEC can also contains a colon and a format
                        specifier, see "--format" for example. FORMULA must be
                        a valid python expression. For each column, list of
                        values to aggregate are accessible as local variable.
                        The formula should return a single value. e.g.
                        "sum(a_colname) + sum(other_colname)". See "--type"
                        for typing other columns and "--before" for run code
                        before evaluating expression. Can be specified
                        multiple time.
  -k KEYS, --key KEYS   Column used groupping the aggregate. Can be specified
                        multiple time. Similar to "GROUP BY" in SQL.

Examples:
  Keeping unique lines, one line per group:
    csvspoon aggregate \
            -k group \
            file.csv

  Computing the total mean grade:
    csvspoon aggregate \
            --np \
            -t grade:float \
            -a meangrade "np.mean(grade)" \
            file.csv

  Computing the total mean grade specifing a format:
    csvspoon aggregate \
            --np \
            -t grade:float \
            -a meangrade:.2f "np.mean(grade)" \
            file.csv

  Computing the mean grade by group:
    csvspoon aggregate \
            --np \
            -t grade:float \
            -a meangrade "np.mean(grade)" \
            -k group \
            file.csv

  Computing the mean grade, median, standard deviation by group:
    csvspoon aggregate \
            --np \
            -t grade:float \
            -a meangrade "np.mean(grade)" \
            -a mediangrade "np.median(grade)" \
            -a stdgrade "np.std(grade)" \
            -k group \
            file.csv

Cli example

csvspoon cat: Concatenate CSV files

  • Change delimiter of a csv file:
csvspoon cat -d "\t" -u ";" file.csv > result.csv
  • Change delimiter of a csv file with specified output:
csvspoon cat -o result.csv -d "\t" -u ";" file.csv
  • Cat two csv files:
csvspoon cat file1.csv file2.csv
  • Reformat two columns of a csv files:
csvspoon cat -f a_colname:5.1f -f another_colname:04d file.csv
  • Cat one csv file, keeping only a column:
csvspoon cat file.csv:a_col
  • Cat two csv files, renaming a column on the second file:
csvspoon cat file1.csv file2.csv:new_col=old_col,another_col

csvspoon apply: Apply functions to add columns

  • Combine text columns by a formula:
csvspoon apply -a name "lastname.upper()+' '+firstname.lower()" file.csv
  • Sum to integer columns:
csvspoon apply -t cola:int -t colb:int -a colsum "cola+colb" file.csv
  • Sum to integer columns and format the result:
csvspoon apply -t cola:int -t colb:int -a colsum:05d "cola+colb" file.csv
  • Compute complex expression between columns:
csvspoon apply \
        -b "import math" \
        -t x:float \
        -t y:float \
        -a norm "math.sqrt(x**2+y**2)" \
        file.csv
  • Multiple computation can be done reusing newly created columns:
csvspoon apply -t x:int -a x2p1 "x**2+1" -a x2p1m1 "x2p1-1" file.csv

csvspoon sort: Sort CSV file

  • Sort csv file using column cola:
csvspoon sort -k cola file.csv
  • Sort csv file using columns cola and colb:
csvspoon sort -k cola -k colb file.csv
  • Sort csv file using numerical mode on column numcol:
csvspoon sort -n -k numcol file.csv
  • Shuffle csv file:
csvspoon sort -R file.csv

csvspoon filter: Filter CSV from given conditions

  • Filter csv file using two columns:
csvspoon filter -a "lastname!=firstname" file.csv
  • Chain filters on csv file:
csvspoon filter \
        -a "lastname.startswith('Doe')" \
        -a "firstname.starswith('John')" \
        file.csv
  • Filter csv file with float column price:
csvspoon filter -t price:float -a "price>12.5" file.csv
  • Filter csv file with complex expression:
csvspoon filter \
        -b "import math" \
        -t x:float \
        -t y:float \
        -t z:float \
        -a "math.sqrt(x**2+y**2)>z" \
        file.csv

csvspoon join: Join CSV files

  • Operate NATURAL JOIN on two csv files:
csvspoon join file1.csv file2.csv
  • Operate two NATURAL JOIN on three csv files:
csvspoon join file1.csv file2.csv file3.csv
  • Operate LEFT JOIN on two csv files
csvspoon join -l file1.csv file2.csv
  • Operate RIGHT JOIN on two csv files
csvspoon join -r file1.csv file2.csv
  • Operate OUTER JOIN on two csv files
csvspoon join -lr file1.csv file2.csv

csvspoon aggregate: Compute aggregation on CSV file

  • Keeping unique lines, one line per group:
csvspoon aggregate \
        -k group \
        file.csv
  • Computing the total mean grade:
csvspoon aggregate \
        --np \
        -t grade:float \
        -a meangrade "np.mean(grade)" \
        file.csv
  • Computing the total mean grade specifing a format:
csvspoon aggregate \
        --np \
        -t grade:float \
        -a meangrade:.2f "np.mean(grade)" \
        file.csv
  • Computing the mean grade by group:
csvspoon aggregate \
        --np \
        -t grade:float \
        -a meangrade "np.mean(grade)" \
        -k group \
        file.csv
  • Computing the mean grade, median, standard deviation by group:
csvspoon aggregate \
        --np \
        -t grade:float \
        -a meangrade "np.mean(grade)" \
        -a mediangrade "np.median(grade)" \
        -a stdgrade "np.std(grade)" \
        -k group \
        file.csv

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csvspoon-0.7.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

csvspoon-0.7-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file csvspoon-0.7.tar.gz.

File metadata

  • Download URL: csvspoon-0.7.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for csvspoon-0.7.tar.gz
Algorithm Hash digest
SHA256 13fb9807dd8a93498b53e862a9b5abebeb6f2f83c5bb9a6dfe289a2ec0ab6d7d
MD5 e7231fa6665dc8eaf9b2d2f2548e99b9
BLAKE2b-256 89dfdc1f5f334d98fb858d456c566efc67ed8999030c2e3b4016291c76256fa9

See more details on using hashes here.

File details

Details for the file csvspoon-0.7-py3-none-any.whl.

File metadata

  • Download URL: csvspoon-0.7-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for csvspoon-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 658535ebe304369496bfce5b2b6fe3e6a89c2d52282005b96e2f3b5b4a2ab8e6
MD5 0f1f4b5f9661109c304c1a8e8582fb9d
BLAKE2b-256 64ef98b58ec00d12d4ca23973fbc2608e21339373a43e488d786ae638b2a6cd2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page