1

I'm attempting to read the result of an API call via Python and I'm using a base application / script as a starting point.

I'm a bit confused at the one point in the script where the reading occurs:

result=urllib2.urlopen.read()[13:-1]
                    if result=="true":
                            logging.info('available')

My question specifically deals with the 13:-1. Per the Python documentation, it appears to be an offset or starting point for the application to read. Here is an example given:

>>> f.seek(-3, 2) # Go to the 3rd byte before the end

This looks slightly different from the .read() in my script as the delimiter in mine is a : rather than a ,.

And to clarify what it is reading, I'm using the Ello API as an example. So it would read a link that generates results that look like this:

{
available: true,
suggestions: [
"crunchyfeelsfists",
"crunchy_nicksshakes",
"supercrunchy"
]
}

So the example script I'm using is reading the "available: true" line.

My first question is, what is the difference between using the : and ,. My next question would be is how is the 13:-1 reading the second line of that API result?

Any help would be appreciated!

amy
  • 185
  • 1
  • 3
  • 13
  • *what is the difference between using the : and ,.* they are not in any way related and cannot be compared this way. One is a function call, the other is a list slicing operation – Tim May 19 '15 at 18:54

4 Answers4

4

you should not be manually seeking like that through a json response object. you should use the json builtin library

just do

data = json.loads(urllib2.urlopen.read())
print data["available"]
print data["suggestions"]
...

especially since there is probably little to no guarantee that "available" will always be the first key output in the json response (since dictionaries are inherently not ordered)

or simply use the requests library

data =requests.get(my_endpoint,data=my_data,headers=my_header).json()

I know this does not address OP's original slicing question (the other answers address that just fine)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

The array notation in the square brackets has to do with the data returned by read(), not as parameters to seek(). [13:-1] means start at the 13th byte and stop and the second-to-last byte.

It's skipping over the response data ("available: ") and the comma (the last byte), looking to see if the response is "true".

Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
0

A slight improvement to @Joran Beasley would be handling exceptions:

try:
    data = json.loads(urllib2.urlopen.read())
    print data.get('avaiable', 'A default value if available wasnt passed')
    # etc etc
except ValueError:
    print "JSON Decoding failed. No data."
Community
  • 1
  • 1
Diogo Martins
  • 917
  • 7
  • 15
0

The [:] syntax denotes a slice. See @Greg Hewgill answer on Explain Python's slice notation for more information on slicing. The result of the slice is what is returned to you.

As for the distinction between a slice and the (,) syntax:

Per the Python documentation

To change the file object’s position, use f.seek(offset, from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

In this context .seek() is a method that takes 2 parameters offset and from_what. When you call on a method, you use a , to separate the arguments.

Community
  • 1
  • 1
rogergarrison
  • 1,224
  • 7
  • 7