TypeError: unsubscriptable object

K

k.retheesh

Can anybody tell me why am I getting this error message while trying to
print a part of a string. Is there a better approach for this...

Traceback (most recent call last):
File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "Q:\PythonScripts\InventoryCompareCase.py", line 276, in ?
main()
File "Q:\PythonScripts\InventoryCompareCase.py", line 167, in main
print template % (ID, IID, Function[:10], Description[:10],
ErrorNumber, StatusCD)
TypeError: unsubscriptable object

Thanks
Retheesh
 
K

K.S.Sreeram

print template % (ID, IID, Function[:10], Description[:10],
ErrorNumber, StatusCD)
TypeError: unsubscriptable object

It means either 'Function' or 'Description' is not a sequence.
Try inserting print statements to see what values they are.

e.g:

a = 2
a[:10]

will give me an 'unsubscriptable object'

Regards
Sreeram


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEibQfrgn0plK5qqURAvU+AJwIDY3uvG5xh7O4+KnkcHjlINla2wCbBXSR
11DRLQgLy4ffXmOvntW6FNg=
=iPC6
-----END PGP SIGNATURE-----
 
K

k.retheesh

So wat should I do ??

K.S.Sreeram said:
print template % (ID, IID, Function[:10], Description[:10],
ErrorNumber, StatusCD)
TypeError: unsubscriptable object

It means either 'Function' or 'Description' is not a sequence.
Try inserting print statements to see what values they are.

e.g:

a = 2
a[:10]

will give me an 'unsubscriptable object'

Regards
Sreeram


--------------enig08E562E3E8ED90BF5BC3C92B
Content-Type: application/pgp-signature
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="signature.asc"
Content-Description: OpenPGP digital signature
X-Google-AttachSize: 253
 
K

k.retheesh

So wat should I do ??

K.S.Sreeram said:
print template % (ID, IID, Function[:10], Description[:10],
ErrorNumber, StatusCD)
TypeError: unsubscriptable object

It means either 'Function' or 'Description' is not a sequence.
Try inserting print statements to see what values they are.

e.g:

a = 2
a[:10]

will give me an 'unsubscriptable object'

Regards
Sreeram


--------------enig08E562E3E8ED90BF5BC3C92B
Content-Type: application/pgp-signature
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="signature.asc"
Content-Description: OpenPGP digital signature
X-Google-AttachSize: 253
 
L

Laszlo Nagy

(e-mail address removed) írta:
So wat should I do ??
You should do this:

print type(Function),type(Description)
raise SystemExit

Instead of this:

print template % (ID, IID, Function[:10], Description[:10],ErrorNumber, StatusCD)

Then probably you will see:

<type 'int'> <type 'int'>

instead of

<type 'str'> <type 'str'>


or something similar. (Of course in your case, it can be 'float instead of list' or 'dictionary instead of tuple' etc.)

Then you should debug your program and find out why the 'Function' and 'Description' objects are not string but int.


Laszlo
 
K

k.retheesh

So wat should I do ??

K.S.Sreeram said:
print template % (ID, IID, Function[:10], Description[:10],
ErrorNumber, StatusCD)
TypeError: unsubscriptable object

It means either 'Function' or 'Description' is not a sequence.
Try inserting print statements to see what values they are.

e.g:

a = 2
a[:10]

will give me an 'unsubscriptable object'

Regards
Sreeram


--------------enig08E562E3E8ED90BF5BC3C92B
Content-Type: application/pgp-signature
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="signature.asc"
Content-Description: OpenPGP digital signature
X-Google-AttachSize: 253
 
K

k.retheesh

I did the same,

The function type is < NoneType> and the description type is <Unicode>
so how can i print a part of this 2 output.

These values are returned by database.

Thanks
Retheesh

Laszlo said:
(e-mail address removed) írta:
So wat should I do ??
You should do this:

print type(Function),type(Description)
raise SystemExit

Instead of this:

print template % (ID, IID, Function[:10], Description[:10],ErrorNumber, StatusCD)

Then probably you will see:

<type 'int'> <type 'int'>

instead of

<type 'str'> <type 'str'>


or something similar. (Of course in your case, it can be 'float instead of list' or 'dictionary instead of tuple' etc.)

Then you should debug your program and find out why the 'Function' and 'Description' objects are not string but int.


Laszlo
 
L

Laszlo Nagy

(e-mail address removed) írta:
I did the same,

The function type is < NoneType> and the description type is <Unicode>
so how can i print a part of this 2 output.
The unicode object IS subscriptable. It is a unicode string which has
characters.
The value None is not subscriptable. It probably means that you have a
NULL value in your database.
Try the following:

def nulltoemptystr(value):
if value is None:
return "<EMPTY>"
else:
return value[:10]

print template % (ID, IID, nulltoemptystr(Function), nulltoemptystr(Description),ErrorNumber, StatusCD)


You can also try to do the same in SQL. For example, this works with
FireBird, PostgreSQL and many others:

select coalesce(Function,"") as Function from TableName

instead of

select Function from TableName

But first of all, I would recommend you to go over the Python tutorial.
It explains these things and more.

http://docs.python.org/tut/tut.html

Best,

Laszlo
 
K

k.retheesh

I did the same,

The function type is < NoneType> and the description type is <Unicode>
so how can i print a part of this 2 output.

These values are returned by database.

Thanks
Retheesh

Laszlo said:
(e-mail address removed) írta:
So wat should I do ??
You should do this:

print type(Function),type(Description)
raise SystemExit

Instead of this:

print template % (ID, IID, Function[:10], Description[:10],ErrorNumber, StatusCD)

Then probably you will see:

<type 'int'> <type 'int'>

instead of

<type 'str'> <type 'str'>


or something similar. (Of course in your case, it can be 'float instead of list' or 'dictionary instead of tuple' etc.)

Then you should debug your program and find out why the 'Function' and 'Description' objects are not string but int.


Laszlo
 
D

Diez B. Roggisch

Can anybody tell me why am I getting this error message while trying to
print a part of a string. Is there a better approach for this...

Because you don't use a string? The error message is pretty clear:

TypeError: unsubscriptable object

So - what are Function[:10], Description[:10] ? You _assume_ they are
strings, seems not to be the case.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top