Permalink
Browse files

Add support for some command line options

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...
1 parent 73fc8e0 commit 4d8e3d1288194c22433f112f2fe504c9e772ca85 @thomasnyman committed Oct 11, 2012
Showing with 38 additions and 11 deletions.
  1. +9 −4 README.md
  2. +29 −7 bin/csv2json
View
@@ -12,16 +12,21 @@ Description
-----------
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
-fieldnames.
+of objects. By default the values in the first row of the CSV data will be used
+as fieldnames.
Options
-------
Mandatory arguments for long options are mandatory for short options too
- -o, --outfile=OUTFILE write output to OUTFILE
- -h, --help show this help message and exit
+ -o, --outfile=OUTFILE write output to OUTFILE
+ -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
--------
View
@@ -14,13 +14,29 @@ def main():
dest='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()
try:
infile = open(args[0], 'r') if len(args) > 0 else sys.stdin
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:
parser.error("I/O error({0}): {1}: {2}".format(e.errno, args[0], e.strerror))
@@ -29,16 +45,22 @@ def main():
if infile: infile.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.
- :infile: Input CSV file
- :outfile: Output JSON file
- :returns: None
+ :infile: Input CSV file.
+ :outfile: Output JSON file.
+ :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)
- json.dump([row for row in reader], fp=outfile)
+ reader = csv.DictReader(infile, fieldnames=fieldnames)
+ json.dump([row for row in reader], fp=outfile, indent=indent, encoding=encoding)
if __name__ == '__main__':
main()

0 comments on commit 4d8e3d1

Please sign in to comment.