-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonlds1.py
205 lines (183 loc) · 7.23 KB
/
jsonlds1.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import csv
import json
import pandas as pd
def convertToJsonldFunction():
# Read Excel file into pandas DataFrame
df = pd.read_excel('test1.xlsx')
# Convert DataFrame to CSV file
df.to_csv('test1.csv', index=False)
# Define the CSV file name
filename ="test1.csv"
# Define the field names
fieldnames = [
"id_",
"type_",
"dateObserved__type_",
"dateObserved__value_",
"airQualityLevel__type_",
"airQualityLevel__value_",
"CO__type_",
"CO__value_",
"CO__unitCode_",
"O3__type_",
"O3__value_",
"O3__unitCode_",
"temp__type_",
"temp__value_",
"PM10__type_",
"PM10__value_",
"PM10__unitCode_",
"PM2.5__type_",
"PM2.5__value_",
"PM2.5__unitCode_",
"C6H6__type_",
"C6H6__value_",
"C6H6__unitCode_",
"NO2__type_",
"NO2__value_",
"NO2__unitCode_",
"NO__type_",
"NO__value_",
"NO__unitCode_",
"SO2__type_",
"SO2__value_",
"SO2__unitCode_",
"refPointOfInterest__type_",
"refPointOfInterest__object_",
"windDirection__type_",
"windDirection__value_",
"windSpeed__type_",
"windSpeed__value_",
"source__type_",
"source__value_",
"location__type_",
"location__value__type_",
"location__value__coordinates__0_",
"location__value__coordinates__1_",
"address__type_",
"address__value__addressCountry_",
"address__value__addressLocality_",
"address__value__streetAddress_",
"address__value__type_",
"relativeHumidity__type_",
"relativeHumidity__value_",
"@context__0_",
"@context__1_"
]
# Define an empty list to hold the JSON objects
json_list = []
# Open the CSV file and read its contents
with open('test1.csv', 'r') as f:
reader = csv.reader(f)
data = [row for row in reader]
headers = data[0]
count=0
# Read the CSV file and create a list of dictionaries
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
if count==0:
count+=1
continue
else:
# Convert each row to a nested dictionary
json_dict = {
"id": row["id_"],
"type": row["type_"],
"dateObserved": {
"type": row["dateObserved__type_"],
"value": row["dateObserved__value_"]
},
"airQualityLevel": {
"type": row["airQualityLevel__type_"],
"value": row["airQualityLevel__value_"]
},
"CO": {
"type": row["CO__type_"],
"value": float(row["CO__value_"]),
"unitCode": row["CO__unitCode_"]
},
"O3": {
"type": row["O3__type_"],
"value": float(row["O3__value_"]),
"unitCode": row["O3__unitCode_"]
},
"temperature": {
"type": row["temp__type_"],
"value": float(row["temp__value_"])
},
"PM10": {
"type": row["PM10__type_"],
"value": float(row["PM10__value_"]),
"unitCode": row["PM10__unitCode_"]
},
"C6H6": {
"type": row["C6H6__type_"],
"value": float(row["C6H6__value_"]),
"unitCode": row["C6H6__unitCode_"]
},
"NO2": {
"type": row["NO2__type_"],
"value": float(row["NO2__value_"]),
"unitCode": row["NO2__unitCode_"]
},
"NO": {
"type": row["NO__type_"],
"value": float(row["NO__value_"]),
"unitCode": row["NO__unitCode_"]
},
"SO2": {
"type": row["SO2__type_"],
"value": float(row["SO2__value_"]),
"unitCode": row["SO2__unitCode_"]
},
"refPointOfInterest":{
"type": row["refPointOfInterest__type_"],
"object": row["refPointOfInterest__object_"]
},
"windDirection":{
"type": row["windDirection__type_"],
"value": float(row["windDirection__value_"])
},
"windSpeed":{
"type": row["windSpeed__type_"],
"value": float(row["windSpeed__value_"])
},
"source":{
"type": row["source__type_"],
"value": row["source__value_"]
},
"location":{
"type": row["location__type_"],
"value": {
"type": row["location__value__type_"],
"coordinates":
(float(row["location__value__coordinates__0_"]),float(row["location__value__coordinates__1_"])),
}
},
"address": {
"type": row["address__type_"],
"value": {
"addressCountry": row["address__value__addressCountry_"],
"addressLocality": row["address__value__addressLocality_"],
"streetAddress": row["address__value__streetAddress_"],
"type": row["address__value__type_"]
}
},
"relativeHumidity":{
"type": row["relativeHumidity__type_"],
"value": float(row["relativeHumidity__value_"])
},
"@context":
[
row["@context__0_"],
row["@context__1_"],
]
}
json_list.append(json_dict)
json_object = json.dumps(json_list, indent=4)
# Writing to sample.json
with open("sample4.json", "w") as outfile:
outfile.write(json_object)
# with open('json_data2.json', 'w') as outfile:
# json.dump(json_data.replace(" ",""), outfile)