Skip to content

Commit

Permalink
Add support for some command line options
Browse files Browse the repository at this point in the history
The --fieldnames option allows to specify CSV fieldnames.
The --indent enables pretty-printing with specified indent level.
The --encoding allows to specify encoding of the CSV data.
  • Loading branch information
thomasnyman committed Oct 11, 2012
1 parent 73fc8e0 commit 4d8e3d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ Description
----------- -----------


The `csv2json` script reads CSV formatted data and outputs it into an JSON array The `csv2json` script reads CSV formatted data and outputs it into an JSON array
of objects. The values in the first row of the CSV data will be used as of objects. By default the values in the first row of the CSV data will be used
fieldnames. as fieldnames.


Options Options
------- -------


Mandatory arguments for long options are mandatory for short options too Mandatory arguments for long options are mandatory for short options too


-o, --outfile=OUTFILE write output to OUTFILE -o, --outfile=OUTFILE write output to OUTFILE
-h, --help show this help message and exit -h, --help show this help message and exit
-f, --fieldnames FIELDNAMES comma separated list of field names, if omitted the
first row of FILE will be used as fieldnames
-i, --indent INDENT integer specifying indent level for pretty-printing,
if zero or negative only newlines will be inserted
-e, --encoding ENCODING use specified ENCODING [default UTF-8]


Examples Examples
-------- --------
Expand Down
36 changes: 29 additions & 7 deletions bin/csv2json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@ def main():
dest='outfile', dest='outfile',
help='write output to OUTFILE') help='write output to OUTFILE')


parser.add_option('-f', '--fieldnames',
dest='fieldnames',
help='comma separated list of field names, if omitted the first row of FILE will be used as fieldnames')

parser.add_option('-i', '--indent',
dest='indent',
default=None,
type='int',
help='integer specifying indent level for pretty-printing, if zero or negative only newlines will be inserted')

parser.add_option('-e', '--encoding',
dest='encoding',
default='UTF-8',
help='use specified ENCODING [default %default]')

(options, args) = parser.parse_args() (options, args) = parser.parse_args()


try: try:
infile = open(args[0], 'r') if len(args) > 0 else sys.stdin infile = open(args[0], 'r') if len(args) > 0 else sys.stdin
outfile = open(options.outfile, 'w') if options.outfile != None else sys.stdout outfile = open(options.outfile, 'w') if options.outfile != None else sys.stdout
fieldnames = options.fieldnames.split(',') if options.fieldnames != None else None


csv2json(infile, outfile) csv2json(infile, outfile, fieldnames, options.indent, options.encoding)


except IOError as e: except IOError as e:
parser.error("I/O error({0}): {1}: {2}".format(e.errno, args[0], e.strerror)) parser.error("I/O error({0}): {1}: {2}".format(e.errno, args[0], e.strerror))
Expand All @@ -29,16 +45,22 @@ def main():
if infile: infile.close() if infile: infile.close()
if outfile: outfile.close() if outfile: outfile.close()


def csv2json(infile, outfile): def csv2json(infile, outfile, fieldnames=None, indent=None, encoding='UTF-8'):
""" """
Reads CSV formatted data from infile and writes JSON formatted data to outfile. Reads CSV formatted data from infile and writes JSON formatted data to outfile.
:infile: Input CSV file :infile: Input CSV file.
:outfile: Output JSON file :outfile: Output JSON file.
:returns: None :fieldnames: List of CSV field names.
If omitted the first row of infile will be used as fieldnames.
:indent: Integer specifying indent level for pretty-printing.
If zero or negative only newlines will be inserted.
None (the default) disables pretty-printing.
:encoding: Infile encoding, default is UTF-8.
:returns: None
""" """
reader = csv.DictReader(infile) reader = csv.DictReader(infile, fieldnames=fieldnames)
json.dump([row for row in reader], fp=outfile) json.dump([row for row in reader], fp=outfile, indent=indent, encoding=encoding)


if __name__ == '__main__': if __name__ == '__main__':
main() main()

0 comments on commit 4d8e3d1

Please sign in to comment.