Skip to main content

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

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')

# For multiple files, either feed them in as a list:
output = converter.convert(input_file_name=['path-to-file-one.csv', 'path-to-file-two.csv'])

#or re-use the same converter with the "append_mode" flag set to True -- this will make it remember all previous files
converter = CSVConverter(config_file_name='path-to-config.json', append_mode=True)
output_of_file_one = converter.convert(input_file_name='path-to-file-one.csv')
output_of_file_one_and_two = converter.convert(input_file_name='path-to-file-two.csv')

Input Config

This goes at the same level as the headers, with the special name "$input_config$"

This supports setting the file format type. Useful if importing an xlsx file that for some reason doesn't have the right filename. Use format for this.

Can also be used to specify which line the header is actually on.

header_line_number is one indexed to line up with the numbers on spreadsheet software.

header_hints is a list of strings that should match to help find the header.

If both are specified, it will start looking for the header line on the line specified.

{
  "$input_config$": {
      "format": "xlsx", // "ods" to be supported later.
      "header_line_number": 3,
      "header_hints": ["Old Header Zero", "Old Header One"],
      "header_hints_in_order": null, // not implemented yet
      "header_hints_together": null // not implemented yet
  },
  "New Header Zero": {
    "old_column": "Old Header Zero",
    "default": "DEFAULT FOR ZERO"
  }
}

JSON Config Generator

A basic config can be generated from a template csv file. This is useful for example when importing product data to a shop. Simply run:

from csv_converter import CSVConverter
CSVConverter(no_config=True).generate_json_headers(input_file_name="import-template.csv",
                                                   output_file_name="import-config.json")

This will create a file called "import-config.json" that will map all the headers in "import-template.csv" to themselves, allowing you to easily go through each one and change the "old_column" values where needed to match your raw data.

Metadata

Release files for csv-converter 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for csv-converter 0.0.7
File Size Uploaded
csv_converter-0.0.7.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for csv-converter 0.0.7
File Interpreter ABI Platform
csv_converter-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / csv_converter-0.0.7.tar.gz

Download URL csv_converter-0.0.7.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b91f3b2a3990f9f4ce57f6988566df2f23af3349827959824aa0e73506bf64d2
BLAKE2b-256 checksum
How to use checksums
bdba1a1c2d5d944376b4532c4892324554b9acb76e9fb002b672014805e847d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

Release files / csv_converter-0.0.7-py3-none-any.whl

Download URL csv_converter-0.0.7-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a76952a6b9abc9c23c3e55d21689ab6362b8955abbd7b8a55f42df1d0a615c70
BLAKE2b-256 checksum
How to use checksums
eef835e08025e4e62eada64797687c26950faf875ebce84646b6cd8e98ead62b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.23.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page