List conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yawgmoth7

    #1

    List conversion

    Hello, I have a piece of code:

    command = raw_input("comm and> ")
    words = string.split(co mmand, ' ')
    temparg = words
    if len(words)<= 3:
    temparg = words[4:]
    else:
    temparg = words[5:]
    funcarg = string.upper(te mparg)
    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(funca rg\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.
    --
    gurusnetwork.or g ( Note, it's not dead..under heavy contruction).
    Gurus'Network - Are you a guru?
    /me goes off and codes.
  • bruno at modulix

    #2
    Re: List conversion

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

    Well, I'm sorry to have to say this, but it doesn't look fine at all:
    [color=blue][color=green][color=darkred]
    >>> words = "one two three".split()
    >>> words[/color][/color][/color]
    ['one', 'two', 'three'][color=blue][color=green][color=darkred]
    >>> len(words)[/color][/color][/color]
    3[color=blue][color=green][color=darkred]
    >>> words[4:][/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> import string
    >>> string.upper(wo rds[4:])[/color][/color][/color]
    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'[color=blue][color=green][color=darkred]
    >>> str(words[4:])[/color][/color][/color]
    '[]'[color=blue][color=green][color=darkred]
    >>> words[4:][/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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 ?-)
    [color=blue]
    > well, then I have a function that looks something like:
    >
    > def nick(funcarg):
    > sock.send(funca rg\r\n)[/color]

    Either it's not your real code or you should have another error:
    [color=blue][color=green][color=darkred]
    >>> def send(aString):[/color][/color][/color]
    .... print "sending %s" % aString
    ....[color=blue][color=green][color=darkred]
    >>> send(words[4:]\r\n)[/color][/color][/color]
    File "<stdin>", line 1
    send(words[4:]\r\n)
    ^
    SyntaxError: invalid token[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    [color=blue]
    > 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,[/color]

    This is called "programmin g by accident", and it's the worst thing to
    do. Don't "hope", try and make sure:
    [color=blue][color=green][color=darkred]
    >>> str(words)[/color][/color][/color]
    "['one', 'two', 'three']"[color=blue][color=green][color=darkred]
    >>> words[/color][/color][/color]
    ['one', 'two', 'three'][color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> " ".join(word s)[/color][/color][/color]
    'one two three'

    [color=blue]
    > instead of being a list, does anyone have any
    > suggestions,[/color]

    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.[color=blue]
    > --[/color]

    <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@xiludom. gro'.split('@')])"

    Comment

    Working...