0

How to catch integers in dictionary keys, most of them string, but some is integers that i need to remove.

I tried this:

def print_words(filename):
    dict = create_dict(filename)
    for key, val in sorted(dict.items()):

        # Integer filter here! 
        # if not isinstance(dict.key, int)     Something wrong here!

        print '{1:^5}\t{0:<}'.format(key.encode('utf-8'), val)
    return
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127

1 Answers1

0

You already have key and val in your loop, dict.key is wrong.

You can use this instead:

{k:v for k, v in d.items() if not isinstance(k, int)}

Or, just replace the line that doesn't work for you with:

if not isinstance(key, int):
    # key is not integer
Maroun
  • 94,125
  • 30
  • 188
  • 241