List conversion

Y

yawgmoth7

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, then I have a function that looks something like:

def nick(funcarg):
sock.send(funcarg\r\n)
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, instead of being a list, does anyone have any
suggestions, thanks, bye.
 
B

bruno at modulix

yawgmoth7 said:
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:
.... print "sending %s" % aString
....
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:
'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
 

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

No members online now.

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top