Skip to main content

A command-line utility and Python API for manipulating CSV data, eg plucking columns and reordering them. It's a bit like the unix utility 'cut'

Project description

A simple wrapper around Python’s CSV module to provide a command-line tool for filtering columns from a CSV file. This is useful as standard tools like awk can’t easily handle the quoting and escaping used in CSV files.

Basically, it’s a bit like cut but for CSVs.

Install

From PyPi:

pip install csvfilter

Use

Pluck fields 1, 3 and 5 from in.csv:

csvfilter -f 1,3,5 in.csv > out.csv

Pluck all fields apart from column 2 from STDIN:

cat in.csv | csvfilter -f 2 -i > out.csv

Convert pipe-separated file to comma-separated (by default, output is comma-separated):

csvfilter -d"|" in.psv > out.csv

Skip that pesky header row:

cat in.csv | csvfilter --skip=1

As you can see, CSV data can be supplied through STDIN or by running csvfilter directly on a file.

Help is in the usual place:

$ csvfilter --help

Usage: csvfilter [options] [inputfile]

Source: https://github.com/codeinthehole/csvfilter/

Options:
-h, --help            show this help message and exit
-f FIELDS, --fields=FIELDS
                        Specify which fields to pluck
-s SKIP, --skip=SKIP  Number of rows to skip
-d DELIMITER, --delimiter=DELIMITER
                        Delimiter of incoming CSV data
-q QUOTECHAR, --quotechar=QUOTECHAR
                        Quotechar of incoming CSV data

-i, --inverse         Invert the filter - ie drop the selected fields
--out-delimiter=OUT_DELIMITER
                        Delimiter to use for output
--out-quotechar=OUT_QUOTECHAR
                        Quote character to use for output

Report issues

Use the Github issue tracker or, better still…

Contribute

After cloning, install the testing requirements:

pip install -r requirements.txt

Install the package in ‘develop’ mode to bring the executable onto your path:

./setup.py develop

Run the tests with:

nosetests

and, if it helps, use the fixture files to test your amendments:

cat fixtures/au.csv | csvfilter -f 3,1,2 -s 1
csvfilter fixutres/au.csv -f 1,2 -i

Have fun.

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

csvfilter-0.3.1.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file csvfilter-0.3.1.tar.gz.

File metadata

  • Download URL: csvfilter-0.3.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for csvfilter-0.3.1.tar.gz
Algorithm Hash digest
SHA256 281f2a3317e28f1c9423105ac93cea93105553437a50b56ab53b23110ce42654
MD5 212b04b081276faf790f5d8a661b5553
BLAKE2b-256 0375acc4463724acdc3d70d9ff9105878019e24208447553f836f06324221673

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