Skip to main content

A clever CSV parser

Project description

CleverCSV: A Clever CSV Parser

Build Status PyPI version Documentation Status Binder

This package is currently in beta. If you encounter any problems, please open an issue or submit a pull request!

Handy links:

Introduction

  • CSV files are awesome: they are lightweight, easy to share, human-readable, version-controllable, and supported by many systems and tools!
  • CSV files are terrible: they can have many different formats, multiple tables, headers or no headers, escape characters, and there's no support for data dictionaries.

CleverCSV is a Python package that aims to solve many of the pain points of CSV files, while maintaining many of the good things. The package automatically detects (with high accuracy) the format (dialect) of CSV files, thus making it easier to simply point to a CSV file and load it, without the need for human inspection. In the future, we hope to solve some of the other issues of CSV files too.

CleverCSV is based on science. We investigated thousands of real-world CSV files to find a robust way to automatically detect the dialect of a file. This may seem like an easy problem, but to a computer a CSV file is simply a long string, and every dialect will give you some table. In CleverCSV we use a technique based on the patterns of the parsed file and the data type of the parsed cells. With our method we achieve a 97% accuracy for dialect detection, with a 21% improvement on non-standard (messy) CSV files.

We think this kind of work can be very valuable for working data scientists and programmers and we hope that you find CleverCSV useful (if there's a problem, please open an issue!) Since the academic world counts citations, please cite CleverCSV if you use the package. Here's a BibTeX entry you can use:

@article{van2018wrangling,
    title={Wrangling Messy {CSV} Files by Detecting Row and Type Patterns},
    author={{van den Burg}, G. J. J. and Naz{\'a}bal, A. and Sutton, C.},
    journal={arXiv preprint arXiv:1811.11242},
    year={2018}
}

And of course, if you like the package please spread the word! You can do this by Tweeting about it (#CleverCSV) or clicking the ⭐️ on GitHub!

Installation

The package is available on PyPI:

$ pip install clevercsv

Usage

CleverCSV consists of a Python library and a command line tool (clevercsv).

Library

We designed CleverCSV to provide a drop-in replacement for the built-in CSV module, with some useful functionality added to it. Therefore, if you simply want to replace the builtin CSV module with CleverCSV, you only have to add one letter:

import clevercsv

CleverCSV provides an improved version of the dialect sniffer in the CSV module, but it also adds some useful wrapper functions. For instance, there's a wrapper for loading a CSV file using Pandas, that uses CleverCSV to detect the dialect of the file:

from clevercsv import csv2df

df = csv2df("data.csv")

Of course, you can also use the traditional way of loading a CSV file, as in the Python CSV module:

# importing this way makes it easy to port existing code to CleverCsv
import clevercsv as csv

with open("data.csv", "r", newline="") as fp:
  # you can use verbose=True to see what CleverCSV does:
  dialect = csv.Sniffer().sniff(fid.read(), verbose=False)
  fp.seek(0)
  reader = csv.reader(fp, dialect)
  rows = list(reader)

That's the basics! If you want more details, you can look at the code of the package or the test suite. Documentation will be provided in the future (but a lot of the functionality is similar to the CSV package in Python!)

Command-Line Tool

The clevercsv command line application has a number of handy features to make working with CSV files easier. For instance, it can be used to view a CSV file on the command line while automatically detecting the dialect. It can also generate Python code for importing data from a file with the correct dialect. The full help text is as follows:

USAGE
  clevercsv [-h] [-v] [-V] <command> [<arg1>] ... [<argN>]

ARGUMENTS
  <command>       The command to execute
  <arg>           The arguments of the command

GLOBAL OPTIONS
  -h (--help)     Display this help message.
  -v (--verbose)  Enable verbose mode.
  -V (--version)  Display the application version.

AVAILABLE COMMANDS
  code            Generate Python code for importing the CSV file.
  detect          Detect the dialect of a CSV file
  help            Display the manual of a command
  standardize     Convert a CSV file to one that conforms to RFC-4180.
  view            View the CSV file on the command line using TabView

Each of the commands has further options (for instance, the code command can generate code for importing a Pandas DataFrame). Use clevercsv help <command> for more information. Below are some examples for each command:

Code

Code generation is useful when you don't want to detect the dialect of the same file over and over again. You simply run the following command and copy the generated code to a Python script!

$ clevercsv code imdb.csv

# Code generated with CleverCSV

import clevercsv

with open("imdb.csv", "r", newline="", encoding=None) as fp:
    reader = clevercsv.reader(fp, delimiter=",", quotechar="", escapechar="\")
    rows = list(reader)

We also have a version that reads a Pandas dataframe:

$ clevercsv code --pandas imdb.csv

# Code generated with CleverCSV

import clevercsv

df = clevercsv.csv2df("imdb.csv", delimiter=",", quotechar="", escapechar="\")

Detect

Detection is useful when you only want to know the dialect.

$ clevercsv detect imdb.csv
Detected: SimpleDialect(',', '', '\\')

The --plain flag gives the components of the dialect on separate lines, which makes combining it with grep easier.

$ clevercsv detect --plain imdb.csv
delimiter = ,
quotechar =
escapechar = \

Standardize

Use the standardize command when you want to rewrite a file using the RFC-4180 standard:

$ clevercsv standardize --output imdb_standard.csv imdb.csv

In this particular example the use of the escape character is replaced by using quotes.

View

This command allows you to view the file in the terminal. The dialect is of course detected using CleverCSV! Both this command and the standardize command support the --transpose flag, if you want to transpose the file before viewing or saving:

$ clevercsv view --transpose imdb.csv

Contributors

Code:

Scientific work:

Contributing

If you want to encourage development of CleverCSV, the best thing to do now is to spread the word!

If you encounter an issue in CleverCSV, please open an issue or submit a pull request!

Notes

License: MIT (see LICENSE file).

Copyright (c) 2019 The Alan Turing Institute.

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

clevercsv-0.3.3.tar.gz (75.5 kB view details)

Uploaded Source

Built Distribution

clevercsv-0.3.3-py3.7-linux-x86_64.egg (126.8 kB view details)

Uploaded Source

File details

Details for the file clevercsv-0.3.3.tar.gz.

File metadata

  • Download URL: clevercsv-0.3.3.tar.gz
  • Upload date:
  • Size: 75.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.3

File hashes

Hashes for clevercsv-0.3.3.tar.gz
Algorithm Hash digest
SHA256 285df730c9b9d23b54abd8dd99051d9436c18aaa1acf47b83126d763cae68d8f
MD5 e49120b5b8c511861585d870f8dddaae
BLAKE2b-256 5af5027aff246f2ca9a85a7a8aabba255230f7431805dadb5397a355fe7b9362

See more details on using hashes here.

File details

Details for the file clevercsv-0.3.3-py3.7-linux-x86_64.egg.

File metadata

  • Download URL: clevercsv-0.3.3-py3.7-linux-x86_64.egg
  • Upload date:
  • Size: 126.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.3

File hashes

Hashes for clevercsv-0.3.3-py3.7-linux-x86_64.egg
Algorithm Hash digest
SHA256 d53f422bba608ea7f69290845a57714f82d5ac8fa6fa9c6d273212dc98d290e4
MD5 c0da2c2d11820b2da9f3b8a63c454e60
BLAKE2b-256 9f96be82bc52319ebf709eeb957560516841a9d687f82ba4e44a73aadcd66841

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