Skip to content

Instantly share code, notes, and snippets.

@akshayjh
Forked from d4rk8l1tz/convert_csv_to_json
Created April 11, 2017 14:25
Show Gist options
  • Save akshayjh/f610ac4108af9ccb630a6e3791c8ea9a to your computer and use it in GitHub Desktop.
Save akshayjh/f610ac4108af9ccb630a6e3791c8ea9a 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