-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_to_csv.py
33 lines (27 loc) · 968 Bytes
/
json_to_csv.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
# pylint: disable=invalid-name
"""
python ./json_to_csv.py
-f <YOUR FILENAME>
"""
# *********************************************************************************************************************
# standard imports
# third party imports
import click
import pandas
import json
# custom imports
@click.command()
@click.option('-f', '--input_filename', type=str, required=True, help='Input File')
def main(input_filename: str):
file = open(input_filename, mode = "r", encoding = "utf8")
workspace_json = json.load(file)
file.close()
df = pandas.json_normalize(workspace_json["examples"])
print(df)
assert input_filename.endswith(".json")
output_filename = input_filename.replace(".json", "_output.csv")
assert input_filename != output_filename
df.to_csv(output_filename,index=False, header=True)
print(f'wrote to: {output_filename}')
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter