Skip to content

Commit

Permalink
Provide Python scripts to process Json files
Browse files Browse the repository at this point in the history
You can use `python script/ProcessJson.py path/to/config.json` to
execute, If you don't provide the file path, it will default to be
`../config.json`.
  • Loading branch information
luckygalaxy666 committed Jul 25, 2024
1 parent cb9246d commit 103304b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "noname",
"sex": "nosex",
"age": 10
}
20 changes: 20 additions & 0 deletions script/ProcessJson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import argparse
import json
from types import SimpleNamespace

def loadJsonAsObject(file_path: str):
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)

# transmit dictronary into object
return json.loads(json.dumps(data), object_hook=lambda d: SimpleNamespace(**d))

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Process JSON file.")
parser.add_argument('file_path', nargs='?', default='../config.json', help="Path to the JSON file")
args = parser.parse_args()
a = loadJsonAsObject(args.file_path)

print(f"name: {a.name}")
print(f"sex: {a.sex}")
print(f"age: {a.age}")

0 comments on commit 103304b

Please sign in to comment.