Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Tool to convert CSV files based on JSON config.

Project description

A simple tool that uses a JSON config file (or a config dict) to convert one CSV to another.

Simplest use case is to just list the new headers and link them to the old headers, like so:

{
"New Column Header One": "Old Column Header One",
"New Column Header Two": "Old Column Header Two"
}

A slightly more useful version specifies a default to use (implemented using the "or" operator value_from_old_column or default):

{
  "New Header One": {
    "old_column": "Old Header One",
    "default": "DEFAULT VALUE FOR ONE"
  },
  "New Header Two": {
    "old_column": "Old Header Two",
    "default": "DEFAULT VALUE FOR TWO"
  }
}

The third and final currently implemented option is to use a lambda function (use of full functions is planned):

{
  "New Header One": {
    "old_column": "Old Header One",
    "default": "DEFAULT FOR ONE",
    "lambda": "lambda a, b: str(a.get(b.get('old_column'))).lower()"
  },
  "New Header Two": {
    "old_column": "Old Header Two",
    "default": "DEFAULT FOR TWO",
    "lambda": "lambda a, b: str(a.get(b.get('old_column'))).lower()"
  }
}

It will run the lambda as:

item['lambda'] = "lambda a, b: str(a.get(b.get('old_column'))).lower()"
exec(f"c = {item.get('lambda')}", globals())
return c(line, item)

Where line is the current line of the source CSV, and item is the current element from the JSON file (the current column).

Usage:

from csv_converter import CSVConverter
converter = CSVConverter(config_file_name='path-to-config.json')
output = converter.convert(input_file_name='path-to-source.csv')

Project details


Download files

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

Files for csv-converter, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size csv_converter-0.0.4-py3-none-any.whl (4.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size csv_converter-0.0.4.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page