-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvToJson.py
44 lines (39 loc) · 1.43 KB
/
csvToJson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import csv
import json
from copy import deepcopy
def makeJsonSample(row, length): ## Simple function for making Json Instance
sample = '{'
for count in range(length):
sample += '"'+ row[count] +'":' + '""'
if(count == len(row)-1):
sample+= '}'
else:
sample+=','
return sample
def convertCsvToJson(fileFrom, fileTo, num_lim = None):
with open(fileFrom, 'r') as file:
reader = csv.reader(file)
row1 = next(reader) # gets the first line
# data = '{ "id":"", "premise":, "hypothesis":""}'
length = len(row1) # gets length of first line
res = makeJsonSample(row1,length) # create Json Instance
jsonSample = json.loads(res) # loads as Json Object
# print(jsonSample)
arr = []
for row in reader:
jsonSampleCopy = deepcopy(jsonSample) # Make copy of Json Instance
for j in range(length):
jsonSampleCopy[row1[j]] = row[j].strip()
print(jsonSampleCopy,end='\n') ##For Testing and see input in colab uncomment
arr.append(jsonSampleCopy)
if(num_lim != None):
if(reader.line_num == num_lim):
break
with open(fileTo,'w+') as json_file:
json.dump(arr,json_file,ensure_ascii= False)
json_file.close()
file.close()
csvFile = "train.csv"
jsonFile = "newJsonFile1.json"
num_lim = 10 ##For Testing and see input in colab uncomment it and if condition below
convertCsvToJson(csvFile, jsonFile, num_lim)