#!/usr/bin/env python
import sys
import csv
import json
import re

if len(sys.argv) != 3:
  print 'Incorrect number of arguments.'
  print 'Usage: csv_to_json.py path_to_csv path_to_json'
  exit()

print 'Argument List:', str(sys.argv)
csvFileName = sys.argv[1]
jsonFileName = sys.argv[2]

csvFile = open (csvFileName, 'rU')
myReader = csv.reader(csvFile)
header = myReader.next()
print "Header fields:", header

myReader = csv.DictReader( csvFile, fieldnames = header) 

jsonFile = open( jsonFileName, 'w')  

# far too fancy regex for my tastes.
# grabs words, makes the tag the last word prior to ".csv"
fileTag = re.findall(r"[\w']+", csvFileName)[-2:-1][0]

def writeNRecords(n):
  count = 0
  jsonFile.write("{\"docs\": [")
  for row in myReader:
    if count != 0:
      jsonFile.write(", ")
    row['tag'] = fileTag
    parsedJson = json.dumps( row )
    jsonFile.write(parsedJson)
    count += 1
    if 0 == (count % n):
      break
  jsonFile.write("] }")

writeNRecords(10000)

print "up to 10,000 JSON records saved to: ", jsonFileName  


@@ -259,28 +259,29 @@
 %5B1%5D%0AjsonFile
-Name
+Array
  = sys.argv%5B
@@ -282,16 +282,27 @@
 .argv%5B2%5D
+.split(%22.%22)
 %0A%0AcsvFil
@@ -481,47 +481,8 @@
 ) %0A%0A
-jsonFile = open( jsonFileName, 'w')  %0A%0A
 # fa
@@ -629,16 +629,35 @@
 -1%5D%5B0%5D%0A%0A
+jsonFileCount = 0%0A%0A
 def writ
@@ -670,16 +670,142 @@
 rds(n):%0A
+  jsonFileName = jsonFileArray%5B0%5D + %22_%22 + str(jsonFileCount) + %22.%22 + jsonFileArray%5B1%5D%0A  jsonFile = open( jsonFileName, 'w')  %0A
   count 
@@ -1081,31 +1081,10 @@
 %7D%22)%0A
-%0AwriteNRecords(10000)%0A%0A
+  
 prin
@@ -1139,10 +1139,102 @@
 leName  
+%0A  return (jsonFileCount + 1)%0A%0Afor x in range(1,250):%0A  jsonFileCount = writeNRecords(10000)
 %0A%0A

rework to loop 250 times max over a file spitting out an incremented filename each time. needs constants refactored but ready to post data.