Last active
August 15, 2016 19:19
-
-
Save chaonan99/3dcaca32258a4f49dfc21f94541b23af to your computer and use it in GitHub Desktop.
View the abstract of a large json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
[Description] View the abstract of a large json file | |
[Author] chaonan99 | |
[Date] 08/15/2016 | |
[Contact] [email protected] | |
""" | |
import json, argparse, sys | |
def view_dict(in_dict, num, spaces): | |
print spaces + "{" | |
i = 0 | |
for k,v in in_dict.items(): | |
if i > num: | |
print spaces + "..." | |
break | |
sys.stdout.write(spaces + k + ":") | |
if type(v) is list: | |
sys.stdout.write("\n") | |
view_list(v, num, spaces + " ") | |
elif type(v) is dict: | |
sys.stdout.write("\n") | |
view_dict(v, num, spaces + " ") | |
else: | |
print spaces + str(v) | |
i += 1 | |
print spaces + "}" | |
def view_list(in_list, num, spaces): | |
print spaces + "[" | |
i = 0 | |
for v in in_list: | |
if i > num: | |
print spaces + "..." | |
break | |
if type(v) is list: | |
view_list(v, num, spaces + " ") | |
elif type(v) is dict: | |
view_dict(v, num, spaces + " ") | |
else: | |
print spaces + str(v) | |
i += 1 | |
print spaces + "]" | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--input_json', required=True, help='input json file') | |
parser.add_argument('-n', '--head_number', default = 6, type=int, help='number of items for abstract') | |
args = parser.parse_args() | |
jf = json.load(open(args.input_json, 'r')) | |
if type(jf) is list: | |
view_list(jf,5,"") | |
else: | |
view_dict(jf,5,"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment