Created
March 21, 2018 16:46
-
-
Save nbrendler/fbe794ae1082dfeb11720ea2e93eb05e to your computer and use it in GitHub Desktop.
CSV to JSONL script using pandas & click
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import json | |
import sys | |
csv.field_size_limit(sys.maxsize) | |
import click | |
import pandas as pd | |
stdin = click.get_binary_stream('stdin') | |
stdout = click.get_text_stream('stdout') | |
@click.command() | |
@click.option('--chunksize', default=100) | |
@click.argument('csv_file', type=click.File('rbU')) | |
def csv2jsonl(csv_file, chunksize): | |
for chunk in pd.read_csv(csv_file, chunksize=chunksize): | |
chunk.to_json(stdout, orient='records', lines=True) | |
if __name__=='__main__': | |
csv2jsonl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drop it somewhere on your path and you can run either
or