Commit 287a6f3b by arjun.b

json parsing

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$" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ 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.9" 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.iml" filepath="$PROJECT_DIR$/.idea/JSON.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
from scripts.config.application_config import json_path
from scripts.core.handlers.json_melt import json_melt
from scripts.core.handlers.json_merge import json_merge
from scripts.core.handlers.json_parsing import json_parsing
from scripts.core.handlers.json_pivot import json_pivot
print("json parsing.........")
json_parsing()
print("pivot based on timestamp......")
json_pivot()
print("melting the data.....")
json_melt()
print("merging the two files")
json_merge()
[excel_file]
excel_path=scripts\utils\task3.xlsx
[json_file]
json_path=scripts\utils\json_data.json
[pivot_file]
pivot_json_path=scripts\utils\pivot_json_data.json
[melt_file]
melt_json_path=scripts\utils\melt_json_data.json
[merge_file]
merge_json_path=scripts\utils\merge_json_data.json
import configparser
config = configparser.ConfigParser()
config.read("conf/application.conf")
file = config.get('excel_file', 'excel_path')
json_path = config.get('json_file', 'json_path')
pivot_path = config.get('pivot_file', 'pivot_json_path')
melt_path = config.get('melt_file', 'melt_json_path')
merge_path = config.get('merge_file', 'merge_json_path')
import json
import pandas as pd
from scripts.config.application_config import file, melt_path
def json_melt():
try:
data = pd.read_excel(file)
melt_json = pd.melt(data, id_vars=["current"], value_vars=["kVA"])
# melting the data based on current and kVA
body = {"melting the data based on current and kVA": melt_json.to_dict(orient='records')}
with open(melt_path, 'w') as f:
json.dump(body, f, indent=4)
except Exception as e:
print(str(e))
import json
import pandas as pd
from scripts.config.application_config import file, merge_path
def json_merge():
try:
data = pd.read_excel(file)
split_one = data[["current", "kWh"]]
split_two = data[["current", "kVA"]]
merge_data = pd.merge(split_one, split_two, on="current")
body = {"merge file based on current ": merge_data.to_dict(orient='records')}
with open(merge_path, 'w') as f:
json.dump(body, f, indent=4)
except Exception as e:
print(str(e))
import json
import pandas as pd
from scripts.config.application_config import file_path, json_path
def json_parsing():
try:
data = pd.read_excel(file_path)
# print(data)
# first 10 rows
first_ten_rows = data.iloc[:10]
# print(first_ten_rows)
# last 10 rows
last_ten_rows = data.iloc[-10:]
# print(last_ten_rows)
# concatenation of first and nad 10 rows
dataframe = pd.concat([first_ten_rows, last_ten_rows], axis=0, ignore_index=True)
#
dataframe['Timestamp'] = dataframe["Timestamp"].apply(lambda x: pd.to_datetime(x, unit='ms')
.strftime('%Y-%m-%d %H:%M:%S'))
# convert dataframe into JSON file
header = list(dataframe.columns)
keys = ['column1', 'column2', 'column2', 'column3', 'column5', 'column6']
body = {"body": dataframe.to_dict(orient='records')}
json_header = {'header': dict(zip(keys, header))}
json_dict = {**json_header, **body}
with open(json_path, 'w') as data:
json.dump(json_dict, data, indent=4)
except Exception as e:
print(str(e))
import json
import pandas as pd
from scripts.config.application_config import file, pivot_path
def json_pivot():
try:
data = pd.read_excel(file)
pivot_json = data.pivot_table(index='Timestamp', columns=[], values=data)
body = {"pivot based on Timestamp": pivot_json.to_dict(orient='records')}
with open(pivot_path, 'w') as f:
json.dump(body, f, indent=4)
# read json file
# with open(pivot_path, 'r') as f:
# data = json.load(f)
# print(json.dumps(data, indent=4))
except Exception as e:
print(str(e))
{
"header": {
"column1": "Timestamp",
"column2": "kVAh",
"column3": "kW",
"column5": "kVA",
"column6": "current"
},
"body": [
{
"Timestamp": "2019-12-01 21:00:00",
"kWh": 46.228215767638176,
"kVAh": 49.759336099588836,
"kW": 183.66897265625,
"kVA": 200.53249609375,
"current": 281.58292388916016
},
{
"Timestamp": "2019-12-01 21:15:00",
"kWh": 45.76348547717498,
"kVAh": 50.2282157676309,
"kW": 183.46255208333332,
"kVA": 200.56794270833333,
"current": 281.617431640625
},
{
"Timestamp": "2019-12-01 21:30:00",
"kWh": 45.76348547718226,
"kVAh": 50.49377593360987,
"kW": 184.88955078125,
"kVA": 201.79479687499997,
"current": 283.0400848388672
},
{
"Timestamp": "2019-12-01 21:45:00",
"kWh": 46.552716320351465,
"kVAh": 50.48294589788566,
"kW": 184.3258984375,
"kVA": 201.42360937499998,
"current": 282.44676971435547
},
{
"Timestamp": "2019-12-01 22:00:00",
"kWh": 46.00458310000249,
"kVAh": 50.13828877951164,
"kW": 182.37034895833335,
"kVA": 199.271421875,
"current": 279.42852783203125
},
{
"Timestamp": "2019-12-01 22:15:00",
"kWh": 46.110687022897764,
"kVAh": 50.63358778625843,
"kW": 184.18944140625,
"kVA": 201.03991015625,
"current": 281.9737777709961
},
{
"Timestamp": "2019-12-01 22:30:00",
"kWh": 46.47655815858161,
"kVAh": 50.83407914363488,
"kW": 186.40050520833333,
"kVA": 203.86802604166667,
"current": 285.9699300130208
},
{
"Timestamp": "2019-12-01 22:45:00",
"kWh": 46.296574167587096,
"kVAh": 50.86875390756177,
"kW": 185.91884374999998,
"kVA": 203.10286718749998,
"current": 285.10118103027344
},
{
"Timestamp": "2019-12-01 23:00:00",
"kWh": 46.59622562891309,
"kVAh": 51.31205402872001,
"kW": 185.94254687499998,
"kVA": 203.36761458333334,
"current": 285.23462931315106
},
{
"Timestamp": "2019-12-01 23:15:00",
"kWh": 46.76348547718226,
"kVAh": 50.514522821576975,
"kW": 185.391484375,
"kVA": 202.44480468749998,
"current": 283.9910583496094
},
{
"Timestamp": "2019-12-02 18:30:00",
"kWh": 46.56323514282849,
"kVAh": 50.29440005487413,
"kW": 184.82915104166668,
"kVA": 202.09330729166666,
"current": 283.2255350748698
},
{
"Timestamp": "2019-12-02 18:45:00",
"kWh": 45.987603305780794,
"kVAh": 50.71900826445926,
"kW": 184.39455078125002,
"kVA": 201.15467187500002,
"current": 281.9291687011719
},
{
"Timestamp": "2019-12-02 19:00:00",
"kWh": 46.71440921193425,
"kVAh": 50.687471904282575,
"kW": 185.46109374999997,
"kVA": 202.85015625,
"current": 284.17213439941406
},
{
"Timestamp": "2019-12-02 19:15:00",
"kWh": 45.83848677928472,
"kVAh": 50.32816541953798,
"kW": 184.31598958333333,
"kVA": 201.36305208333332,
"current": 282.21966552734375
},
{
"Timestamp": "2019-12-02 19:30:00",
"kWh": 46.59999657076696,
"kVAh": 50.61659408114065,
"kW": 184.44642578124999,
"kVA": 201.67296484375,
"current": 282.61204528808594
},
{
"Timestamp": "2019-12-02 19:45:00",
"kWh": 46.07635197695345,
"kVAh": 50.9892150474916,
"kW": 182.8969296875,
"kVA": 199.87789062499996,
"current": 280.1493377685547
},
{
"Timestamp": "2019-12-02 20:00:00",
"kWh": 45.95020746887894,
"kVAh": 50.03734439834079,
"kW": 182.440359375,
"kVA": 199.39240234375,
"current": 279.73744201660156
},
{
"Timestamp": "2019-12-02 20:15:00",
"kWh": 46.498233942598745,
"kVAh": 50.41558931449981,
"kW": 184.44230729166668,
"kVA": 201.8952552083333,
"current": 283.39756266276044
},
{
"Timestamp": "2019-12-02 20:30:00",
"kWh": 45.89180755117559,
"kVAh": 50.27735674359428,
"kW": 183.25395312499998,
"kVA": 200.42523828125,
"current": 281.13245391845703
},
{
"Timestamp": "2019-12-02 20:45:00",
"kWh": 41.18257261411054,
"kVAh": 44.87966804979078,
"kW": 183.2521640625,
"kVA": 200.4773046875,
"current": 281.26131439208984
}
]
}
\ No newline at end of file
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