-
-
Save akshayjh/f610ac4108af9ccb630a6e3791c8ea9a to your computer and use it in GitHub Desktop.
Python : Convert CSV to JSON (line by line)
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
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