Commit f45ab9b4 by logesh.n

csv to json done

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Json_parsing)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Json_parsing.iml" filepath="$PROJECT_DIR$/.idea/Json_parsing.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import csv
import json
def csv_to_json(csv_file_path, json_file_path):
# create a dictionary
data_dict = {}
# Step 2
# open a csv file handler
with open(csv_file_path, encoding='utf-8') as csv_file_handler:
csv_reader = csv.DictReader(csv_file_handler)
# convert each row into a dictionary
# and add the converted data to the data_variable
for rows in csv_reader:
# assuming a column named 'No'
# to be the primary key
key = rows['SNo']
data_dict[key] = rows
# open a json file handler and use json.dumps
# method to dump the data
# Step 3
with open(json_file_path, 'w', encoding='utf-8') as json_file_handler:
# Step 4
json_file_handler.write(json.dumps(data_dict, indent=4))
# driver code
# be careful while providing the path of the csv file
# provide the file path relative to your machine
# Step 1
csv_file_path = input('Enter the absolute path of the CSV file: ')
json_file_path = input('Enter the absolute path of the JSON file: ')
csv_to_json(csv_file_path, json_file_path)
SNo,Timestamp,kWh,kVAh,kW,kVA,current
1,9:00:00 PM,46.22821577,49.7593361,183.6689727,200.5324961,281.5829239
2,9:15:00 PM,45.76348548,50.22821577,183.4625521,200.5679427,281.6174316
3,9:30:00 PM,45.76348548,50.49377593,184.8895508,201.7947969,283.0400848
4,9:45:00 PM,46.55271632,50.4829459,184.3258984,201.4236094,282.4467697
5,10:00:00 PM,46.0045831,50.13828878,182.370349,199.2714219,279.4285278
6,10:15:00 PM,46.11068702,50.63358779,184.1894414,201.0399102,281.9737778
7,10:30:00 PM,46.47655816,50.83407914,186.4005052,203.868026,285.96993
8,10:45:00 PM,46.29657417,50.86875391,185.9188438,203.1028672,285.101181
9,11:00:00 PM,46.59622563,51.31205403,185.9425469,203.3676146,285.2346293
10,11:15:00 PM,46.76348548,50.51452282,185.3914844,202.4448047,283.9910583
11,11:30:00 PM,46.7912623,50.96601625,185.4971133,202.893375,284.5935898
12,01-12-2019 23:45,46.86088154,51.14325069,186.9712305,204.7621445,287.1810226
13,12:00:00 AM,46.68810512,51.33056708,187.2335677,204.5804583,286.9411825
14,12:15:00 AM,46.73200508,51.18733925,186.0456563,202.8139453,284.3181686
15,12:30:00 AM,46.79496588,51.26079353,186.8331797,204.3109961,286.5990677
16,12:45:00 AM,46.85892116,51.12863071,187.1399102,204.2709375,286.5819321
17,1:00:00 AM,47.14894649,51.88054822,186.9562344,204.2602708,286.5058187
18,1:15:00 AM,47.48590828,51.36011568,186.92925,204.0068555,286.2642593
19,1:30:00 AM,47.09100852,50.81574333,187.3521875,204.5727891,287.0003738
20,1:45:00 AM,46.8301533,50.63238945,186.0468398,203.1011367,285.0034714
21,2:00:00 AM,45.72398066,51.17797744,184.3389375,201.2684948,282.404541
22,2:15:00 AM,46.3553719,50.29752066,184.6342422,201.8070938,283.0306931
23,2:30:00 AM,46.24793388,50.97933884,184.6788633,201.5256367,282.820137
24,2:45:00 AM,46.76814924,50.49951991,184.2325885,201.2694792,282.310201
25,3:00:00 AM,45.77178423,50.50207469,183.580625,200.7382461,281.3659744
26,3:15:00 AM,46.70848452,50.43419157,184.7863125,201.8739492,282.9246216
27,3:30:00 AM,46.00105905,50.73593292,185.7652891,203.5681875,285.0090561
28,3:45:00 AM,46.5852337,51.22910737,184.7759896,202.2692083,283.2581685
29,4:00:00 AM,46.45626007,50.2771167,185.0352969,202.3385234,283.6272812
30,4:15:00 AM,46.11618257,50.22406639,184.8881992,201.9441992,282.904007
31,4:30:00 AM,46.76348548,51.11618257,184.9109375,202.3414063,283.5337219
32,4:45:00 AM,45.77150989,50.50246905,184.7092813,201.8274167,283.0511068
33,5:00:00 AM,46.80525359,51.11163883,184.6931289,201.988832,282.9909058
34,5:15:00 AM,46.06639004,50.22406639,186.5683164,204.0375039,286.0867615
35,5:30:00 AM,46.44140733,51.13850277,184.707875,201.8596094,283.1792603
36,5:45:00 AM,46.78265906,50.54614454,184.230651,201.262401,282.2118937
37,6:00:00 AM,46.28445527,50.52096979,184.7952656,202.1291055,283.3441238
38,6:15:00 AM,46.29230822,50.74873976,185.7647031,202.6981094,283.8277054
39,6:30:00 AM,45.76348548,50.26556017,184.8259896,202.1638958,282.53243
40,6:45:00 AM,46.76348548,51.18257261,184.8970625,202.0656445,282.3910522
41,7:00:00 AM,46.79229107,50.83618532,184.5802461,201.3318242,281.4895248
42,7:15:00 AM,46.24214876,50.92899449,185.7976797,203.4111563,284.364624
43,7:30:00 AM,46.65020747,50.54187414,185.5356615,203.3179531,284.45755
44,7:45:00 AM,46.51673468,51.09111485,186.1933711,203.3625078,284.3076477
45,8:00:00 AM,46.5247591,51.25743287,186.3628906,203.6023125,284.8340454
46,8:15:00 AM,46.49377593,51.22406639,185.9764805,203.1833945,284.1766663
47,8:30:00 AM,47.49377593,51.22406639,186.4182448,203.8110938,285.1260071
48,8:45:00 AM,46.52074689,51.25311203,186.3151016,203.7405,284.8326874
49,9:00:00 AM,46.43723849,50.93723849,186.2049961,203.4351641,283.949173
50,9:15:00 AM,46.61462873,50.57728433,185.295651,203.0170677,283.3728841
51,9:30:00 AM,46.76348548,51.18672199,186.1303594,203.7046406,284.8661957
52,9:45:00 AM,46.21770515,50.83181304,185.5998945,202.961207,284.3711319
53,10:00:00 AM,46.51673468,50.67441103,185.3284844,202.9535039,284.0480423
54,10:15:00 AM,46.60580913,51.22406639,186.3175938,203.9241094,285.1775411
55,10:30:00 AM,46.76348548,51.22406639,186.9348398,204.3435625,285.485817
56,10:45:00 AM,47.1228976,51.21081055,186.2981719,203.9254414,284.9080963
57,11:00:00 AM,46.4656781,50.83473721,185.9884948,203.7065938,284.5499674
58,11:15:00 AM,46.78572559,51.22447041,186.0624102,203.3610469,284.1670303
59,11:30:00 AM,46.79167381,50.95819759,186.8676094,204.143082,285.2875519
60,11:45:00 AM,46.76348548,51.49377593,185.8260664,203.3497227,283.8757019
61,12:00:00 PM,46.49507368,50.46602804,187.0693906,204.6899688,285.6078288
62,12:15:00 PM,47.0816898,51.06924167,186.5178438,203.9836055,284.4987106
63,12:30:00 PM,46.76348548,51.22406639,186.9434922,204.0151992,284.5806808
64,12:45:00 PM,46.72029423,51.2267069,183.9988281,201.5383021,281.3934835
65,1:00:00 PM,45.85646926,50.27951716,183.1253711,200.096832,279.4674225
66,1:15:00 PM,45.76348548,50.22406639,182.4745547,199.5217461,278.4521103
67,1:30:00 PM,45.91286307,50.22406639,183.8831992,200.6780938,280.0551987
68,1:45:00 PM,46.45315301,50.30377542,183.3296823,200.4262396,279.4595032
69,2:00:00 PM,45.97423288,50.58834076,184.0671641,201.4087773,280.7499008
70,2:15:00 PM,46.10788382,50.49377593,184.1131406,201.4959727,280.7490387
71,2:30:00 PM,46.41908714,50.49377593,183.9537656,200.6890273,279.8290939
72,2:45:00 PM,45.77315593,50.31003738,183.1457344,200.3349271,280.4654439
73,3:00:00 PM,46.34302665,50.7314564,185.7474883,203.0448242,284.3976135
74,3:15:00 PM,46.49377593,50.76348548,186.3097344,203.5141055,284.8322296
75,3:30:00 PM,46.44418916,51.16621515,185.9589635,203.9565,285.7725525
76,3:45:00 PM,46.82644628,50.5661157,186.4073828,203.576793,285.2424927
77,4:00:00 PM,46.92561983,51.1446281,185.6820234,202.6229375,284.267395
78,4:15:00 PM,46.78714722,51.25167175,186.0387031,203.1809063,284.955864
79,4:30:00 PM,46.70458832,51.22766709,184.8634167,202.0778958,283.2348429
80,4:45:00 PM,46.54437434,50.60635781,186.2454453,204.0464453,285.9152679
81,5:00:00 PM,46.49377593,50.89626556,185.4037383,202.4774922,283.8940964
82,5:15:00 PM,46.39943402,50.99694439,185.7739414,203.2029727,284.7158051
83,5:30:00 PM,46.72504731,50.58811785,185.0127344,201.8797292,283.0105387
84,5:45:00 PM,46.37869072,51.1106615,185.4774102,202.8813281,284.3863449
85,6:00:00 PM,46.5258736,50.25863311,185.2086055,202.2409453,283.3320618
86,6:15:00 PM,46.43568465,51.1659751,185.4590156,202.1452305,283.3671875
87,6:30:00 PM,46.56323514,50.29440005,184.829151,202.0933073,283.2255351
88,6:45:00 PM,45.98760331,50.71900826,184.3945508,201.1546719,281.9291687
89,7:00:00 PM,46.71440921,50.6874719,185.4610938,202.8501563,284.1721344
90,7:15:00 PM,45.83848678,50.32816542,184.3159896,201.3630521,282.2196655
91,7:30:00 PM,46.59999657,50.61659408,184.4464258,201.6729648,282.6120453
92,7:45:00 PM,46.07635198,50.98921505,182.8969297,199.8778906,280.1493378
93,8:00:00 PM,45.95020747,50.0373444,182.4403594,199.3924023,279.737442
94,8:15:00 PM,46.49823394,50.41558931,184.4423073,201.8952552,283.3975627
95,8:30:00 PM,45.89180755,50.27735674,183.2539531,200.4252383,281.1324539
96,8:45:00 PM,41.18257261,44.87966805,183.2521641,200.4773047,281.2613144
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment