List conversion

bruno at modulix onurb at xiludom.gro
Thu Mar 30 11:14:25 CEST 2006


yawgmoth7 wrote:
> Hello, I have a piece of code:
> 
>                command = raw_input("command> ")
>                words = string.split(command, ' ')
>                temparg = words
>                if len(words)<= 3:
>                        temparg = words[4:]
>                else:
>                        temparg = words[5:]
>                        funcarg = string.upper(temparg)
>                        str(funcarg)
>                        continue
> 
> There's a little snippet of code from my script, it all looks fine,

Well, I'm sorry to have to say this, but it doesn't look fine at all:

>>> words = "one two three".split()
>>> words
['one', 'two', 'three']
>>> len(words)
3
>>> words[4:]
[]
>>> import string
>>> string.upper(words[4:])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib64/python2.4/string.py", line 235, in upper
    return s.upper()
AttributeError: 'list' object has no attribute 'upper'
>>> str(words[4:])
'[]'
>>> words[4:]
[]
>>>

Python comes with an interactive interpreter that makes testing and
exploring a breeze. Why not using it to see how things works and avoid
obvious errors ?-)

> well, then I have a function that looks something like:
> 
> def nick(funcarg):
>        sock.send(funcarg\r\n)

Either it's not your real code or you should have another error:

>>> def send(aString):
...     print "sending %s" % aString
...
>>> send(words[4:]\r\n)
  File "<stdin>", line 1
    send(words[4:]\r\n)
                      ^
SyntaxError: invalid token
>>>


> Well, that's okay, but i get this error when I try to run that command
> at the commmand prompt like enviroment I wrote:
> 
> TypeError: send() argument 1 must be string or read-only buffer, not list
> 
> Well, that is why I put in the str(funcarg) line, hoping that it would
> convert it to a string,

This is called "programming by accident", and it's the worst thing to
do. Don't "hope", try and make sure:

>>> str(words)
"['one', 'two', 'three']"
>>> words
['one', 'two', 'three']
>>>

As you can see, str() :
- returns a *representation* of it's arg as a string - but this
representation may not be what your looking for
- does *not* modify it's argument.

What you want here is to join the parts of the list:

>>> " ".join(words)
'one two three'


> instead of being a list, does anyone have any
> suggestions, 

Yes :
1/ there are at least two good tutorials, the one in the official
documentation and Dive Into Python (diveintopython.org IIRC).

2/ don't guess, don't hope, *test* (hint : use the interactive interpreter).

3/ (optional) don't hold it against me if all this sounds a bit harsh.

thanks, bye.
> --

<OT>
needs a whitespace after the two dashes. It's '-- ', not '--'.
</OT>

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list