[Tutor] Attribute.Error: 'list' object has no attribute strip

Gerrit Holl gerrit@nl.linux.org
Fri Feb 28 15:41:40 2003


vicki@stanfield.net schreef op vrijdag 28 februari om 21:03:44 +0000:
> Thanks for all the help with the listbox callback
> stuff. Now onto something else. I am trying to use the
> strip function on the data which is returned from a
> getcurselection() call on that same listbox. What is
> returned is in the following format (number will vary):
> 
> ('06',)
> 
> I want to get only the number part, so I use the
> following:
> 
> command=string.strip([(),])
> 
> The error I get is 
> 
> Attribute.Error: 'list' object has no attribute strip
> 
> I am including the string module. Can anyone tell me
> what I am missing (this time)?

strip is a string method. Your object is not a string.

If you type the name of an object in your interpreter,
you may get something like ('06',). This does *not*
mean that this is a string containing those characters!
In this case, it is only the *representation* of the
object: for humans, so that it is readable:

 52 >>> t
('06',)

In this case, you probably have a _tuple_, with one _element_,
which is a _string_. You want an _integer_. So you
need to:

    * get the only element from the tuple
    * this element is a string
    * get the integer out of a string

A tuple is a sequence. This time, you want the first
element:

 53 >>> s=t[0]
 54 >>> s
'06'

Now, we have a string containing the characters '06'. But
we want to have an object with the *number* 6, right?
Well, if you are absolutely sure that the string contains
only integers, you can do:

 55 >>> i = int(s)
 56 >>> i
6

Voila! Here we have our integer. Now we are going to write
this in one line:

 57 >>> i = int(t[0])

The relevant documentation can be found online:
 * http://www.python.org/dev/doc/devel/tut/node5.html
 * http://www.python.org/dev/doc/devel/lib/built-in-funcs.html
 * http://www.python.org/dev/doc/devel/lib/typesnumeric.html
 * http://www.python.org/dev/doc/devel/lib/typesseq.html
 * http://www.python.org/dev/doc/devel/ref/parenthesized.html#l2h-241

Hope this helps!

yours,
Gerrit Holl.

-- 
Asperger Syndroom - een persoonlijke benadering:
            http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/