├── json2csv.py ├── Readme.md └── csv2json.py /json2csv.py: -------------------------------------------------------------------------------- 1 | import argparse, os 2 | parser = argparse.ArgumentParser( 3 | description='Converts a JSON file to CSV', 4 | epilog='To avoid ambiguity put the list following flags -u, -n, and -a into quotes. ' + 5 | 'Alternatively, supply infile (and optional outfile) before listing the flags.' 6 | ) 7 | parser.add_argument("infile", default='data.json', nargs='?', help='Input file name, default: data.json') 8 | parser.add_argument("outfile", nargs='?', help='Output file name, default: [infile_basename].csv') 9 | parser.add_argument('-S', '--separator', default=',', help='CSV separator used, default ","') 10 | parser.add_argument('-i', '--index', action='store_true', help='Write row names (index)') 11 | parser.add_argument('-I', '--indexlabel', help='Label for index column') 12 | parser.add_argument('-u', '--usecols', nargs='+', help='List of names of columns to use') 13 | parser.add_argument('-n', '--names', nargs='+', help='Column names to use') 14 | parser.add_argument('-a', '--append', nargs='+', help='Names of columns to append') 15 | parser.add_argument('-p', '--printdata', action='store_true', help='Print formatted data when done') 16 | 17 | args = parser.parse_args() 18 | 19 | infile = args.infile 20 | if not os.path.isfile(infile): 21 | print('File "%s" does not exist, aborting. Use --help to show command syntax and command line options.' % infile) 22 | exit() 23 | 24 | outfile = args.outfile 25 | if outfile is None: 26 | outfile = os.path.splitext(infile)[0] + '.csv' 27 | 28 | print("Converting %s -> %s [sep=%s]" % 29 | (infile, outfile, args.separator)) 30 | if args.usecols is not None: 31 | print("Only using columns: %s" % (' '.join(args.usecols),)) 32 | 33 | 34 | import pandas as pd 35 | try: 36 | df = pd.read_json(infile) 37 | except ValueError: 38 | print('Could not read JSON data from "%s":\n' % infile) 39 | raise 40 | 41 | # Append columns if given 42 | if args.append is not None: 43 | for additional_column in args.append: 44 | print('Adding column: %s' % additional_column) 45 | df[additional_column] = '' 46 | 47 | # Change column names if given 48 | if args.names is not None: 49 | df.columns = args.names 50 | 51 | # Convert 52 | if args.usecols is None: 53 | df.to_csv(outfile, index=args.index, index_label=args.indexlabel, sep=args.separator) 54 | else: 55 | df.to_csv(outfile, index=args.index, index_label=args.indexlabel, sep=args.separator, columns=args.usecols) 56 | 57 | if args.printdata: 58 | print(df) 59 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | ``` 2 | usage: json2csv.py [-h] [-S SEPARATOR] [-i] [-I INDEXLABEL] 3 | [-u USECOLS [USECOLS ...]] [-n NAMES [NAMES ...]] 4 | [-a APPEND [APPEND ...]] [-p] 5 | [infile] [outfile] 6 | 7 | Converts a JSON file to CSV 8 | 9 | positional arguments: 10 | infile Input file name, default: data.json 11 | outfile Output file name, default: [infile_basename].csv 12 | 13 | optional arguments: 14 | -h, --help show this help message and exit 15 | -S SEPARATOR, --separator SEPARATOR 16 | CSV separator used, default "," 17 | -i, --index Write row names (index) 18 | -I INDEXLABEL, --indexlabel INDEXLABEL 19 | Label for index column 20 | -u USECOLS [USECOLS ...], --usecols USECOLS [USECOLS ...] 21 | List of names of columns to use 22 | -n NAMES [NAMES ...], --names NAMES [NAMES ...] 23 | Column names to use 24 | -a APPEND [APPEND ...], --append APPEND [APPEND ...] 25 | Names of columns to append 26 | -p, --printdata Print formatted data when done 27 | 28 | To avoid ambiguity put the list following flags -u, -n, and -a into quotes. 29 | Alternatively, supply infile (and optional outfile) before listing the flags. 30 | ``` 31 | 32 | ``` 33 | usage: csv2json.py [-h] [-S SEPARATOR] [-H HEADERLINE] [-c COLUMNS] 34 | [-u USECOLS [USECOLS ...]] [-n NAMES [NAMES ...]] 35 | [-N NROWS] [-s SKIPROWS] [-r USEROWS [USEROWS ...]] 36 | [-a APPEND [APPEND ...]] [-p] 37 | [infile] [outfile] 38 | 39 | Converts a CSV file to JSON 40 | 41 | positional arguments: 42 | infile Input file name, default: data.csv 43 | outfile Output file name, default: [infile_basename].json 44 | 45 | optional arguments: 46 | -h, --help show this help message and exit 47 | -S SEPARATOR, --separator SEPARATOR 48 | CSV separator used, default "," 49 | -H HEADERLINE, --headerline HEADERLINE 50 | Header line to use as column names 51 | -c COLUMNS, --columns COLUMNS 52 | Number of columns to crop to 53 | -u USECOLS [USECOLS ...], --usecols USECOLS [USECOLS ...] 54 | List of names of columns to use 55 | -n NAMES [NAMES ...], --names NAMES [NAMES ...] 56 | Column names to use 57 | -N NROWS, --nrows NROWS 58 | Number of rows to read 59 | -s SKIPROWS, --skiprows SKIPROWS 60 | Number of rows to skip before reading 61 | -r USEROWS [USEROWS ...], --userows USEROWS [USEROWS ...] 62 | List of rows to use 63 | -a APPEND [APPEND ...], --append APPEND [APPEND ...] 64 | Names of columns to append 65 | -p, --printdata Print formatted data when done 66 | 67 | To avoid ambiguity put the list following flags -u, -n, -r and -a into quotes. 68 | Alternatively, supply infile (and optional outfile) before listing the flags. 69 | ``` 70 | -------------------------------------------------------------------------------- /csv2json.py: -------------------------------------------------------------------------------- 1 | import argparse, os 2 | parser = argparse.ArgumentParser( 3 | description='Converts a CSV file to JSON', 4 | epilog='To avoid ambiguity put the list following flags -u, -n, -r and -a into quotes. ' + 5 | 'Alternatively, supply infile (and optional outfile) before listing the flags.' 6 | ) 7 | parser.add_argument("infile", default='data.csv', nargs='?', help='Input file name, default: data.csv') 8 | parser.add_argument("outfile", nargs='?', help='Output file name, default: [infile_basename].json') 9 | parser.add_argument('-S', '--separator', default=',', help='CSV separator used, default ","') 10 | parser.add_argument('-H', '--headerline', type=int, default=0, help='Header line to use as column names') 11 | parser.add_argument('-c', '--columns', type=int, help='Number of columns to crop to') 12 | parser.add_argument('-u', '--usecols', nargs='+', help='List of names of columns to use') 13 | parser.add_argument('-n', '--names', nargs='+', help='Column names to use') 14 | parser.add_argument('-N', '--nrows', type=int, default=None, help='Number of rows to read') 15 | parser.add_argument('-s', '--skiprows', type=int, default=None, help='Number of rows to skip before reading') 16 | parser.add_argument('-r', '--userows', nargs='+', default=None, help='List of rows to use') 17 | parser.add_argument('-a', '--append', nargs='+', help='Names of columns to append') 18 | parser.add_argument('-p', '--printdata', action='store_true', help='Print formatted data when done') 19 | 20 | args = parser.parse_args() 21 | 22 | infile = args.infile 23 | if not os.path.isfile(infile): 24 | print('File "%s" does not exist, aborting. Use --help to show command syntax and command line options.' % infile) 25 | exit() 26 | 27 | outfile = args.outfile 28 | if outfile is None: 29 | outfile = os.path.splitext(infile)[0] + '.json' 30 | 31 | print("Converting %s -> %s [sep=%s, header=%s]" % 32 | (infile, outfile, args.separator, args.headerline)) 33 | if args.usecols is not None: 34 | print("Only using columns: %s" % (' '.join(args.usecols),)) 35 | 36 | 37 | import pandas as pd 38 | if args.usecols is None: 39 | try: 40 | df = pd.read_csv( 41 | infile, sep=args.separator, header=args.headerline, 42 | nrows=args.nrows, skiprows=args.skiprows, 43 | engine='python' 44 | ) 45 | except Exception: 46 | print('Could not read CSV data from "%s":\n' % infile) 47 | raise 48 | 49 | else: 50 | try: 51 | df = pd.read_csv( 52 | infile, sep=args.separator, header=args.headerline, 53 | usecols=args.usecols, 54 | nrows=args.nrows, skiprows=args.skiprows, 55 | engine='python' 56 | ) 57 | except Exception: 58 | print('Could not read CSV data from "%s":\n' % infile) 59 | raise 60 | 61 | # crop columns 62 | if args.columns: 63 | df = df[df.columns[:args.columns]] 64 | 65 | # remove empty rows 66 | df.dropna(how='all') 67 | 68 | # only keep certain rows if given 69 | if args.userows is not None: 70 | print("Only keeping rows: %s" % (' '.join(args.userows),)) 71 | df = df[df.index.isin([int(row) for row in args.userows])] 72 | 73 | # append columns if given 74 | if args.append is not None: 75 | for additional_column in args.append: 76 | print('Adding column: %s' % additional_column) 77 | df[additional_column] = '' 78 | 79 | # Change column names if given 80 | if args.names is not None: 81 | df.columns = args.names 82 | 83 | df.to_json(outfile, orient='records') 84 | 85 | if args.printdata: 86 | print(df) 87 | --------------------------------------------------------------------------------