Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 73fc8e0b754fd0a6828189b965cf0e6d39148c3b 0 parents
@thomasnyman authored
0  .gitignore
No changes.
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright © 2012 Thomas Nyman
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 README.md
@@ -0,0 +1,29 @@
+csv2json
+========
+
+`csv2json` is simple wrapper script around the Python standard library `csv` and
+`json` modules for converting CSV formatted data to JSON.
+
+Synopsis
+--------
+ csv2json [OPTION...] [FILE]
+
+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.
+
+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
+
+Examples
+--------
+ csv2json < in.csv > out.json
+ csv2json -o out.json in.csv
44 bin/csv2json
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+# encoding: utf-8
+import csv
+import json
+import sys
+from optparse import OptionParser
+
+def main():
+ """Handles command line option parsing and csv2json procedure invocation."""
+ usage = "usage: %prog [OPTION...] [FILE]"
+ parser = OptionParser(usage)
+
+ parser.add_option('-o', '--outfile',
+ dest='outfile',
+ help='write output to OUTFILE')
+
+ (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
+
+ csv2json(infile, outfile)
+
+ except IOError as e:
+ parser.error("I/O error({0}): {1}: {2}".format(e.errno, args[0], e.strerror))
+
+ finally:
+ if infile: infile.close()
+ if outfile: outfile.close()
+
+def csv2json(infile, outfile):
+ """
+ Reads CSV formatted data from infile and writes JSON formatted data to outfile.
+
+ :infile: Input CSV file
+ :outfile: Output JSON file
+ :returns: None
+ """
+ reader = csv.DictReader(infile)
+ json.dump([row for row in reader], fp=outfile)
+
+if __name__ == '__main__':
+ main()
11 setup.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+# encoding: utf-8
+from distutils.core import setup
+
+setup(name='csv2json',
+ description='CSV to JSON conversion utility.',
+ author='Thomas Nyman',
+ author_email='thomas.nyman@cs.helsinki.fi',
+ url='http://github.com/thomasnyman/csv2json',
+ scripts=['bin/csv2json']
+)
Please sign in to comment.
Something went wrong with that request. Please try again.