Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json_operation_python
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rakesh.pv
json_operation_python
Commits
12e099df
Commit
12e099df
authored
Jan 27, 2023
by
rakesh.pv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
build
parent
3753acb7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
58 additions
and
9 deletions
+58
-9
scripts/core/handlers/json_concat.py
+16
-0
scripts/core/handlers/json_parser.py
+32
-1
scripts/core/services/app.py
+10
-8
scripts/temp/task3.csv
+0
-0
scripts/temp/task3.xlsx
+0
-0
No files found.
scripts/core/handlers/json_concat.py
View file @
12e099df
import
json
import
pandas
as
pd
def
json_merge
():
try
:
data
=
pd
.
read_csv
(
"C:/Users/rakesh.pv/PycharmProjects/pythonProject1/scripts/temp/task3.csv"
)
split_one
=
data
[[
"Timestamp"
,
"kWh"
]]
split_two
=
data
[[
"Timestamp"
,
"kVA"
]]
merge_data
=
pd
.
merge
(
split_one
,
split_two
,
on
=
"Timestamp"
)
body
=
{
"merge file based on Timestamp "
:
merge_data
.
to_dict
(
orient
=
'records'
)}
with
open
(
"C:/Users/rakesh.pv/PycharmProjects/pythonProject1/scripts/temp/task3.csv"
,
'w'
)
as
f
:
json
.
dump
(
body
,
f
,
indent
=
4
)
except
Exception
as
e
:
print
(
str
(
e
))
scripts/core/handlers/json_parser.py
View file @
12e099df
import
json
import
json
import
pandas
as
pd
def
json_parsing
():
try
:
data
=
pd
.
read_csv
(
"C:/Users/rakesh.pv/PycharmProjects/pythonProject1/scripts/temp/task3.csv"
)
first_ten_rows
=
data
.
iloc
[:
10
]
last_ten_rows
=
data
.
iloc
[
-
10
:]
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
)
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S'
))
print
(
dataframe
)
# 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
(
"C:/Users/rakesh.pv/PycharmProjects/pythonProject1/scripts/temp/task3.xlsx"
,
'w'
)
as
data
:
json
.
dump
(
json_dict
,
data
,
indent
=
4
)
except
Exception
as
e
:
print
(
str
(
e
))
scripts/core/services/app.py
View file @
12e099df
from
scripts.core.handlers.json_concat
import
json_merge
from
scripts.core.handlers.json_parser
import
json_parsing
choice
=
int
(
input
(
"JSON operations
\n
1.json Parsing
\n
2.json merging
\n
3.melting data
\n
4.concat pivot
\n
Choose your choice
\n
"
))
if
choice
==
1
:
json_parsing
()
choice
=
int
(
input
(
"JSON operations
\n
1.json Parsing
\n
2.json melting
\n
3.melting data
\n
4.concat two data items
\n
Choose your choice
\n
"
))
if
choice
==
1
:
elif
choice
==
2
:
json_merge
()
elif
choice
==
3
:
print
()
elif
choice
==
2
:
print
()
elif
choice
==
3
:
print
()
elif
choice
==
4
:
elif
choice
==
4
:
print
()
else
:
print
(
" invalid option given"
)
scripts/temp/task3.csv
0 → 100644
View file @
12e099df
This diff is collapsed.
Click to expand it.
scripts/temp/task3.xlsx
View file @
12e099df
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment