[Tutor] Using json to filter out results from a dictionary

Steven D'Aprano steve at pearwood.info
Fri Dec 14 00:21:38 CET 2012


On 14/12/12 09:39, Jasmine Gao wrote:
> Hello,
>
> I've just started programming in python and haven't had much experience
> with json.


This is not really a JSON problem. JSON just handles converting data in a
dict to a string and back. As you say:

> I'm stuck in terms of how you loop through the dictionary and only
> print out certain keys while dumping the rest.

That part is simple:

for key, item in mydict.items():
     if key != "rubbish":
         print key, item
         # In python 3, use "print(key, item)" instead


Another way:

if "rubbish" in mydict:
     del mydict
print mydict
# In python 3, use "print(mydict)" instead



> To illustrate, this is the script:
>
> import urllib2
> CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b"
> CLIENT_SECRET = "4...d"  # secret redacted

Should you have showed us this secret? Probably not a great idea.


> ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3"
> BITLY_ENDPOINT = "https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token="+ACCESS_TOKEN
>
> def getServiceResponse():
>      url = BITLY_ENDPOINT
>      request = urllib2.Request(url)
>      response = urllib2.urlopen(request)
>      d = json.loads(response.read())
>
> getServiceResponse()
>
>
> In terminal I run this command: python /file/location/file.py | tr "," "\n"
> which returns a long list of results like this:


Your script doesn't actually print the results or do anything with them.
Perhaps the part of your script that actually handles output is missing?

What other important parts are missing? It makes it hard for us to help
when we have to guess what you are actually doing. My guess is that you
are actually running:

print getServiceResponse()

or something similar.


Since you are already using an external tool "tr", you could simply pipe
the output through grep:

python /file/location/file.py | tr "," "\n" | grep -v ghash


However, instead of using the external "tr" command, you might like to
do this:


import pprint
# ... rest of your script as before

d = getServiceResponse()
pprint.pprint(d)


That my give you acceptable results without the "tr" command. In this
case, you need to filter the dict returned by getServiceResponse. I
suggest something like this may be acceptable:

d = getServiceResponse()
for item in d:
     if isinstance(item, dict) and 'ghash' in item:
         del item['ghash']
pprint.pprint(d)



Does that help?




-- 
Steven


More information about the Tutor mailing list