TypeError: unsubscriptable object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • k.retheesh@gmail.com

    TypeError: unsubscriptable object

    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\Li b\site-packages\python win\pywin\frame work\scriptutil s.py",
    line 310, in RunScript
    exec codeObject in __main__.__dict __
    File "Q:\PythonScrip ts\InventoryCom pareCase.py", line 276, in ?
    main()
    File "Q:\PythonScrip ts\InventoryCom pareCase.py", line 167, in main
    print template % (ID, IID, Function[:10], Description[:10],
    ErrorNumber, StatusCD)
    TypeError: unsubscriptable object

    Thanks
    Retheesh

  • K.S.Sreeram

    #2
    Re: TypeError: unsubscriptable object

    k.retheesh@gmai l.com wrote:[color=blue]
    > print template % (ID, IID, Function[:10], Description[:10],
    > ErrorNumber, StatusCD)
    > TypeError: unsubscriptable object[/color]

    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 'unsubscriptabl e object'

    Regards
    Sreeram


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

    iD8DBQFEibQfrgn 0plK5qqURAvU+AJ wIDY3uvG5xh7O4+ KnkcHjlINla2wCb BXSR
    11DRLQgLy4ffXmO vntW6FNg=
    =iPC6
    -----END PGP SIGNATURE-----

    Comment

    • k.retheesh@gmail.com

      #3
      Re: TypeError: unsubscriptable object

      So wat should I do ??

      K.S.Sreeram wrote:[color=blue]
      > k.retheesh@gmai l.com wrote:[color=green]
      > > print template % (ID, IID, Function[:10], Description[:10],
      > > ErrorNumber, StatusCD)
      > > TypeError: unsubscriptable object[/color]
      >
      > 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 'unsubscriptabl e object'
      >
      > Regards
      > Sreeram
      >
      >
      > --------------enig08E562E3E8E D90BF5BC3C92B
      > Content-Type: application/pgp-signature
      > Content-Transfer-Encoding: base64
      > Content-Disposition: inline;
      > filename="signa ture.asc"
      > Content-Description: OpenPGP digital signature
      > X-Google-AttachSize: 253[/color]

      Comment

      • k.retheesh@gmail.com

        #4
        Re: TypeError: unsubscriptable object

        So wat should I do ??

        K.S.Sreeram wrote:[color=blue]
        > k.retheesh@gmai l.com wrote:[color=green]
        > > print template % (ID, IID, Function[:10], Description[:10],
        > > ErrorNumber, StatusCD)
        > > TypeError: unsubscriptable object[/color]
        >
        > 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 'unsubscriptabl e object'
        >
        > Regards
        > Sreeram
        >
        >
        > --------------enig08E562E3E8E D90BF5BC3C92B
        > Content-Type: application/pgp-signature
        > Content-Transfer-Encoding: base64
        > Content-Disposition: inline;
        > filename="signa ture.asc"
        > Content-Description: OpenPGP digital signature
        > X-Google-AttachSize: 253[/color]

        Comment

        • Laszlo Nagy

          #5
          Re: TypeError: unsubscriptable object

          k.retheesh@gmai l.com írta:[color=blue]
          > So wat should I do ??
          >[/color]
          You should do this:

          print type(Function), type(Descriptio n)
          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



          Comment

          • k.retheesh@gmail.com

            #6
            Re: TypeError: unsubscriptable object

            So wat should I do ??

            K.S.Sreeram wrote:[color=blue]
            > k.retheesh@gmai l.com wrote:[color=green]
            > > print template % (ID, IID, Function[:10], Description[:10],
            > > ErrorNumber, StatusCD)
            > > TypeError: unsubscriptable object[/color]
            >
            > 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 'unsubscriptabl e object'
            >
            > Regards
            > Sreeram
            >
            >
            > --------------enig08E562E3E8E D90BF5BC3C92B
            > Content-Type: application/pgp-signature
            > Content-Transfer-Encoding: base64
            > Content-Disposition: inline;
            > filename="signa ture.asc"
            > Content-Description: OpenPGP digital signature
            > X-Google-AttachSize: 253[/color]

            Comment

            • k.retheesh@gmail.com

              #7
              Re: TypeError: unsubscriptable object

              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 Nagy wrote:[color=blue]
              > k.retheesh@gmai l.com írta:[color=green]
              > > So wat should I do ??
              > >[/color]
              > You should do this:
              >
              > print type(Function), type(Descriptio n)
              > 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[/color]

              Comment

              • Laszlo Nagy

                #8
                Re: TypeError: unsubscriptable object

                k.retheesh@gmai l.com írta:[color=blue]
                > 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.
                >[/color]
                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),Er rorNumber, StatusCD)


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

                select coalesce(Functi on,"") 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.

                Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...


                Best,

                Laszlo

                Comment

                • k.retheesh@gmail.com

                  #9
                  Re: TypeError: unsubscriptable object

                  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 Nagy wrote:[color=blue]
                  > k.retheesh@gmai l.com írta:[color=green]
                  > > So wat should I do ??
                  > >[/color]
                  > You should do this:
                  >
                  > print type(Function), type(Descriptio n)
                  > 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[/color]

                  Comment

                  • Diez B. Roggisch

                    #10
                    Re: TypeError: unsubscriptable object

                    k.retheesh@gmai l.com schrieb:[color=blue]
                    > 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...[/color]

                    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

                    Comment

                    Working...