Skip to content

Instantly share code, notes, and snippets.

@d4rk8l1tz
Created July 9, 2015 16:14
Show Gist options
  • Save d4rk8l1tz/18c66065b9dab734e27f to your computer and use it in GitHub Desktop.
Save d4rk8l1tz/18c66065b9dab734e27f to your computer and use it in GitHub Desktop.
Python : Convert CSV to JSON (line by line)
def convertCSVtoJSON(input): #pass the name of the input csv file
f = open(input, 'r')
j = open('.tempJSON', 'w')
fieldnames = ("field1,field2,field3")
reader = csv.DictReader(f, fieldnames)
for row in reader:
json.dump(row, j)
j.write('\n')
f.close()
j.close()
os.system('rm -f input')
os.system('mv .tempJSON input')
return
#the output is a file in which every line is a JSON document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment