[Tutor] Tutor Digest, Vol 106, Issue 31

Waters, Mike [ITSCA Non-J&J] mwaters at ITS.JNJ.com
Fri Dec 14 14:39:25 CET 2012


Hi Python tutor
I have a question I believe I have posted before: when you have filled the page with text (commands,list etc) how do you clear the page to allow a clean page to continue on writing script?
Thanks appreciate your help.
Mike

-----Original Message-----
From: Tutor [mailto:tutor-bounces+mwaters=its.jnj.com at python.org] On Behalf Of tutor-request at python.org
Sent: Friday, December 14, 2012 6:00 AM
To: tutor at python.org
Subject: Tutor Digest, Vol 106, Issue 31

Send Tutor mailing list submissions to
	tutor at python.org

To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
	tutor-request at python.org

You can reach the person managing the list at
	tutor-owner at python.org

When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..."


Today's Topics:

   1. Using json to filter out results from a dictionary (Jasmine Gao)
   2. Re: Using json to filter out results from a dictionary
      (Steven D'Aprano)


----------------------------------------------------------------------

Message: 1
Date: Thu, 13 Dec 2012 17:39:01 -0500
From: Jasmine Gao <jasminegao93 at gmail.com>
To: tutor at python.org
Subject: [Tutor] Using json to filter out results from a dictionary
Message-ID:
	<CAHY51Lp2UxLeedzaCRnWefSEPF=nn8qzHNcuqqaWkDNmQyMrPw at mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hello,

I've just started programming in python and haven't had much experience with json. I've written this simple script that makes a request to the Bitly API, specifically the /v3/realtime/bursting_phrases<http://dev.bitly.com/data_apis.html#v3_realtime_bursting_phrases>endpoint,
and prints the response into terminal. What I want to do is take the response and filter out a portion of it so I only see the data I find important. So far I've used json to load the results into a dictionary, however I'm stuck in terms of how you loop through the dictionary and only print out certain keys while dumping the rest.

To illustrate, this is the script:

import urllib2


CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b"

CLIENT_SECRET = "404862446e88f391e8c411ca1ee912506d64fffd"

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:


"data": {"selectivity": 3.0

"phrase": "justin bieber"

  "mean": 0.089999999999999997}

  {"std": 0.046334721206249076

  "ghashes": [

 {"visitors": 440

 "ghash": "QXfZfQ"}

 {"visitors": 215

 "ghash": "W9sHrc"}

 {"visitors": 92

 "ghash": "XY9PPX"}]

  "N": 203183.0

  "rate": 0.53000000000000003

    "urls": [

 {"visitors": 440

 "aggregate_url": "http://bit.ly/QXfZfQ"}

 {"visitors": 215

 "aggregate_url": "http://bit.ly/W9sHrc"}

 {"visitors": 92

 "aggregate_url": "http://bit.ly/XY9PPX"}]


How do I use json to filter out the "ghashes" from what is printed into terminal so that the results look like this instead?:

"data": {"selectivity": 3.0

"phrase": "justin bieber"

  "mean": 0.089999999999999997}

  {"std": 0.046334721206249076

  "N": 203183.0

  "rate": 0.53000000000000003

     "urls": [

  {"visitors": 440

 "aggregate_url": "http://bit.ly/QXfZfQ"}

  {"visitors": 215

  "aggregate_url": "http://bit.ly/W9sHrc"}

  {"visitors": 92

 "aggregate_url": "http://bit.ly/XY9PPX"}]


Any help would greatly be appreciated, thank you!

- Jasmine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121213/2faf02a3/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 14 Dec 2012 10:21:38 +1100
From: Steven D'Aprano <steve at pearwood.info>
To: tutor at python.org
Subject: Re: [Tutor] Using json to filter out results from a
	dictionary
Message-ID: <50CA6302.2070707 at pearwood.info>
Content-Type: text/plain; charset=UTF-8; format=flowed

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


------------------------------

Subject: Digest Footer

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


------------------------------

End of Tutor Digest, Vol 106, Issue 31
**************************************



More information about the Tutor mailing list