json_info_show.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. import argparse
  16. def json_info_show(json_path, show_num):
  17. print("Info".center(100, "-"))
  18. print("json read...")
  19. with open(json_path, "r") as load_f:
  20. data = json.load(load_f)
  21. print("json keys:", data.keys(), "\n")
  22. for k, v in data.items():
  23. print(k.center(50, "*"))
  24. show_num_used = show_num if len(v) > show_num else len(v)
  25. if isinstance(v, list):
  26. print(" Content Type: list\n Total Length: %d\n First %d record:\n"
  27. % (len(v), show_num_used))
  28. for i in range(show_num_used):
  29. print(v[i])
  30. elif isinstance(v, dict):
  31. print(" Content Type: dict\n Total Length: %d\n First %d record:\n"
  32. % (len(v), show_num_used))
  33. for i, (kv, vv) in enumerate(v.items()):
  34. if i < show_num_used:
  35. print(kv, ":", vv)
  36. else:
  37. print(v)
  38. print("...\n...\n")
  39. if __name__ == "__main__":
  40. parser = argparse.ArgumentParser(
  41. description="Show information in JSON file")
  42. parser.add_argument("--json_path", type=str, required=True, \
  43. help="Path of the JSON file whose statistics are to be collected.")
  44. parser.add_argument("--show_num", type=int, default=5, \
  45. help="Number of elements to show in the output.")
  46. args = parser.parse_args()
  47. json_info_show(args.json_path, args.show_num)