json_InfoShow.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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'%(len(v),show_num_t))
  34. for i in range(show_num_t):
  35. print(v[i])
  36. elif isinstance(v, dict):
  37. print(' Content Type: dict\n Total Length: %d\n First %d record:\n'%(len(v),show_num_t))
  38. for i,(kv,vv) in enumerate(v.items()):
  39. if i<show_num_t:
  40. print(kv,':',vv)
  41. print('...\n...\n')
  42. def get_args():
  43. parser = argparse.ArgumentParser(description='Json Infomation Show')
  44. # parameters
  45. parser.add_argument('--json_path', type=str,
  46. help='json path to show information')
  47. parser.add_argument('--show_num', type=int, default=5,
  48. help='show number of each sub record')
  49. parser.add_argument('-Args_show', '--Args_show', type=bool, default=True,
  50. help='Args_show(default: True), if True, show args info')
  51. args = parser.parse_args()
  52. if args.Args_show:
  53. print('Args'.center(100,'-'))
  54. for k, v in vars(args).items():
  55. print('%s = %s' % (k, v))
  56. print()
  57. return args
  58. if __name__ == '__main__':
  59. args = get_args()
  60. js_show(args.json_path, args.show_num)