json_InfoShow.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. """
  15. @File Description:
  16. # 输出json文件基本信息
  17. python ./coco_tools/json_InfoShow.py \
  18. --json_path=./annotations/instances_val2017.json \
  19. --show_num 5
  20. """
  21. import json
  22. import argparse
  23. def js_show(js_path, show_num):
  24. print('Info'.center(100, '-'))
  25. print('json read...')
  26. with open(js_path, 'r') as load_f:
  27. data = json.load(load_f)
  28. print('json keys:', data.keys(), '\n')
  29. for k, v in data.items():
  30. print(k.center(50, '*'))
  31. show_num_t = show_num if len(v) > show_num else len(v)
  32. if isinstance(v, list):
  33. print(' Content Type: list\n Total Length: %d\n First %d record:\n'
  34. % (len(v), show_num_t))
  35. for i in range(show_num_t):
  36. print(v[i])
  37. elif isinstance(v, dict):
  38. print(' Content Type: dict\n Total Length: %d\n First %d record:\n'
  39. % (len(v), show_num_t))
  40. for i, (kv, vv) in enumerate(v.items()):
  41. if i < show_num_t:
  42. print(kv, ':', vv)
  43. print('...\n...\n')
  44. def get_args():
  45. parser = argparse.ArgumentParser(description='Json Infomation Show')
  46. # Parameters
  47. parser.add_argument(
  48. '--json_path', type=str, help='json path to show information')
  49. parser.add_argument(
  50. '--show_num',
  51. type=int,
  52. default=5,
  53. help='show number of each sub record')
  54. parser.add_argument(
  55. '-Args_show',
  56. '--Args_show',
  57. type=bool,
  58. default=True,
  59. help='Args_show(default: True), if True, show args info')
  60. args = parser.parse_args()
  61. if args.Args_show:
  62. print('Args'.center(100, '-'))
  63. for k, v in vars(args).items():
  64. print('%s = %s' % (k, v))
  65. print()
  66. return args
  67. if __name__ == '__main__':
  68. args = get_args()
  69. js_show(args.json_path, args.show_num)