Skip to content

Commit 42ac753

Browse files
author
patrick
committed
#2 completed update json to excel
1 parent 315d545 commit 42ac753

File tree

9 files changed

+119
-17
lines changed

9 files changed

+119
-17
lines changed

pyproject.toml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[project]
22
authors = [
33
{ name = "fluentqa-team", email = "fluent-qa@fluentqa.com" },
4-
{name = "fluent", email = "fluentqa@fluentqa.com"},
54
]
65
classifiers = [
76
"Development Status :: 3 - Alpha",
@@ -96,16 +95,16 @@ src = ["src"]
9695

9796
[tool.ruff.lint]
9897
select = [
99-
"B", # flake8-bugbear
100-
"D", # pydocstyle
101-
"E", # pycodestyle error
102-
"F", # Pyflakes
103-
"I", # isort
98+
"B", # flake8-bugbear
99+
"D", # pydocstyle
100+
"E", # pycodestyle error
101+
"F", # Pyflakes
102+
"I", # isort
104103
"RUF100", # Unused noqa directive
105-
"S", # flake8-bandit
106-
"SIM", # flake8-simplify
107-
"UP", # pyupgrade
108-
"W", # pycodestyle warning
104+
"S", # flake8-bandit
105+
"SIM", # flake8-simplify
106+
"UP", # pyupgrade
107+
"W", # pycodestyle warning
109108
]
110109

111110
[tool.ruff.lint.per-file-ignores]
@@ -121,3 +120,6 @@ all = true
121120
in_place = true
122121
trailing_comma_inline_array = true
123122

123+
[build-system]
124+
requires = ["pdm-backend"]
125+
build-backend = "pdm.backend"

src/qpystructs/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
""".editorignore"""
21
__VERSION__ = "0.0.1"
32

4-
from qpystructs.dicttools import *
5-
from qpystructs.models import *
6-
from qpystructs.operations import *
7-
from qpystructs.dotty import *
3+
from .dicttools import *
4+
from .dotty import *
5+
from .models import *
6+
from .operations import *

src/qpystructs/cli/__init__.py

Whitespace-only changes.

src/qpystructs/operations.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import pandas as pd
1616
from deepdiff import DeepDiff
1717

18-
from qpystructs.dotty import dotty
18+
from .dotty import dotty
1919

2020

2121
class DataFormatType(enum.Enum):
@@ -157,3 +157,21 @@ def differ(expected_data: Union[str, dict], actual_data: Union[str, dict]):
157157
else:
158158
v2 = actual_data
159159
return extract_obj_diff(DeepDiff(v1, v2, verbose_level=2))
160+
161+
162+
def json_file_to_excel(json_file, excel_file):
163+
"""
164+
json file to excel
165+
"""
166+
json_data = load_from_file(json_file)
167+
json_data_df = pd.json_normalize(json_data)
168+
json_data_df.to_excel(excel_file)
169+
170+
171+
def read_merged_excel(excel_path):
172+
"""
173+
read merged cell excel file
174+
"""
175+
raw_df = pd.read_excel(excel_path)
176+
raw_df.fillna("fillna", inplace=True)
177+
return raw_df

tests/json-flat.xlsx

5.07 KB
Binary file not shown.

tests/merged_cell.xlsx

9.19 KB
Binary file not shown.

tests/test_operations.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import pandas as pd
2+
3+
from qpystructs.operations import *
14
from tests.helper.data_constants import *
2-
from qpystructs import get_value, set_value, differ, load_from_file
35

46

57
def test_get_value_by_expression():
@@ -36,3 +38,44 @@ def test_query_expression():
3638
assert query_result == {
3739
"WashingtonCities": "Bellevue, Olympia, Seattle"
3840
}
41+
42+
43+
def test_flat_json():
44+
data = [
45+
{
46+
"state": "Florida",
47+
"shortname": "FL",
48+
"info": {"governor": "Rick Scott"},
49+
"counties": [
50+
{"name": "Dade", "population": 12345},
51+
{"name": "Broward", "population": 40000},
52+
{"name": "Palm Beach", "population": 60000},
53+
],
54+
},
55+
{
56+
"state": "Ohio",
57+
"shortname": "OH",
58+
"info": {"governor": "John Kasich"},
59+
"counties": [
60+
{"name": "Summit", "population": 1234},
61+
{"name": "Cuyahoga", "population": 1337},
62+
],
63+
}]
64+
result = pd.json_normalize(
65+
data, "counties", ["state", "shortname", ["info", "governor"]]
66+
)
67+
print(result)
68+
result.to_excel("json-flat.xlsx")
69+
70+
json_data = load_from_file("tmp.json")
71+
print(json_data)
72+
result_json_np = pd.json_normalize(json_data,)
73+
formula_data = json_data['formula']
74+
pd_formula_data = pd.json_normalize(formula_data,'formulaSlots')
75+
pd_formula_data.to_excel('emission-json.xlsx')
76+
77+
78+
def test_merged_cell():
79+
json_file_to_excel("tmp.json",'tmp-json.xlsx')
80+
result = read_merged_excel("merged_cell.xlsx")
81+
print(result)

tests/tmp-json.xlsx

5.01 KB
Binary file not shown.

tests/tmp.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[
2+
{
3+
"state": "Florida",
4+
"shortname": "FL",
5+
"info": {
6+
"governor": "Rick Scott"
7+
},
8+
"counties": [
9+
{
10+
"name": "Dade",
11+
"population": 12345
12+
},
13+
{
14+
"name": "Broward",
15+
"population": 40000
16+
},
17+
{
18+
"name": "Palm Beach",
19+
"population": 60000
20+
}
21+
]
22+
},
23+
{
24+
"state": "Ohio",
25+
"shortname": "OH",
26+
"info": {
27+
"governor": "John Kasich"
28+
},
29+
"counties": [
30+
{
31+
"name": "Summit",
32+
"population": 1234
33+
},
34+
{
35+
"name": "Cuyahoga",
36+
"population": 1337
37+
}
38+
]
39+
}
40+
]

0 commit comments

Comments
 (0)