Python TypeError

Ok, so these lines usually work fine, but sometime produce type error (the bold part is producing it).
 1228         url = "http://www.google.com/" % params
 1229        [b] output = getPage("https://api-ssl.bitly.com/v3/shorten?login=xxx&apiKey=xxx&%s&format=txt"\
 1230         % urllib.urlencode({'longUrl':url}))[:22].replace("http://", "")[/b]

Open in new window


Error:
TypeError: unsubscriptable object
      args = ('unsubscriptable object',)

Also in debug report, when this error occured both params and url variables were setup fine. HELP!
GVNPublic123Asked:
Who is Participating?
 
peprConnect With a Mentor Commented:
OK.  I cannot help too much here.  If you know it was the line
1229  output = getpage(...)[:22].replace("http://", "")[/b]

Open in new window


The error generally means that you try to index/slice what cannot be indexed -- i.e. the object you probably got cannot be sliced using the [:22].  It is likely that the getpage() returned None as the error result or something like that.

Try to avoid that kind of method chaining if the code is a bit complex and if it can fail:
    output = ''         # or some better initial value
    page = getpage(...)
    if page:              # or some more explicit testing
        output = page[:22].replace(...)

Open in new window

0
 
peprCommented:
You should copy/paste our error message with the trace.
0
 
GVNPublic123Author Commented:
But I gave you traced line of code and error...what more do you need? There aint no more in report.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
peprCommented:
How do you run it?  Is it a part of some web server code?  Where the 'args = ...' came from?
0
 
GVNPublic123Author Commented:
No idea...its just a random python fail.
0
 
GVNPublic123Author Commented:
It occured, but I cant recreate it. I put exception handling for next time.
0
 
peprCommented:
How do you run your Python code?  Is it saved in a file and launched say from command line?  How do you know that the mentioned line caused the error?  What version of Python do you use?  What OS do you use?
0
 
GVNPublic123Author Commented:
CentOS 5, its a website, python 2.4.3

Got it from bug report.
0
 
GVNPublic123Author Commented:
Yeh, I put exception handling in so now I dont care if slicing fails. The worker will pick it up later automatically if it fails on request.
0
 
peprCommented:
Sorry I could not be more explicit.  Thanks for the points, and have a good time ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.