Last active
September 27, 2015 02:18
-
-
Save kurthumes/cd8f32a69b0d4e3384ff to your computer and use it in GitHub Desktop.
convert csv file to json using python
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
from optparse import OptionParser | |
import csv | |
import json | |
if __name__ == "__main__": | |
parser = OptionParser(usage="usage: %prog csvfile") | |
parser.add_option("-d", "--delimiter", type="string", dest="delimiter", | |
default=",", help="the delimiter, default is ','") | |
(options, args) = parser.parse_args() | |
if(len(args) != 1): | |
parser.print_help() | |
exit() | |
print json.dumps([record for record in csv.DictReader(open(args[0], "r"), delimiter=options.delimiter)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment