0
import json

def read_json(filename):

    dt = {}

    fh = open(filename, "r")
    dt = json.load(fh)

    return dt


def print_keys(dt):

    print "Keys: ", dt.keys()
    # print "[%s]" % (', ' .join(dt.keys),)


filename = raw_input("Enter the JSON file: ")


r = read_json(filename)
print_keys(r)

I'm trying to print the keys (1 per line) and without the unicode before it. However, I keep getting all the keys in a list in a single line. Please help

Christian König
  • 3,437
  • 16
  • 28

2 Answers2

0
def print_keys(dt):
    print "Keys:"
    for key in dt:
        print key
Christian König
  • 3,437
  • 16
  • 28
0

If you just want to print the keys

for key in dt:
    print "keys: ", key

of course it will not be sorted

If you want to print the keys and the values

for key, value in dt.iteritems():
    print key, value
Riley Hughes
  • 1,344
  • 2
  • 12
  • 22
  • If I want instead to print the objects that are contained in the keys, should I proceed the same way or should I add several print statements? –  Feb 23 '17 at 00:18
  • use built in `iteritems()`. for loop the key, value pair in `dt.iteritems` then `print key, value`. see this answer [here](http://stackoverflow.com/questions/26660654/how-do-i-print-the-key-value-pairs-of-a-dictionary-in-python) – Riley Hughes Feb 23 '17 at 03:41