Disassembling strings and turning them into function parameters

Discussion in 'Python' started by mercuryprey@gmail.com, Jan 30, 2005.

  1. Guest

    Hi,
    I'm pretty new to Python, to programming overall...so how would I make
    something where the user inputs multiple words in a string - like
    "connect 123.123.123.123 21 user password" or similar, and then I can
    split this string up to pass these arguments to a function like
    ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    the string up so I can get these out of it..


    thanks for answers,
    munin
    , Jan 30, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > Hi,
    > I'm pretty new to Python, to programming overall...so how would I make
    > something where the user inputs multiple words in a string - like
    > "connect 123.123.123.123 21 user password" or similar, and then I can
    > split this string up to pass these arguments to a function like
    > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    > the string up so I can get these out of it..


    The .split() method of strings should work for you.

    If you need more, provide more background... and maybe
    let us know that this isn't homework. ;-)

    -Peter
    Peter Hansen, Jan 30, 2005
    #2
    1. Advertising

  3. <> wrote:

    > I'm pretty new to Python, to programming overall...so how would I make
    > something where the user inputs multiple words in a string - like
    > "connect 123.123.123.123 21 user password" or similar, and then I can
    > split this string up to pass these arguments to a function like
    > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    > the string up so I can get these out of it..


    you can use the split() method to split on whitespace:

    >>> s = "connect 123.123.123.123 21 user password"
    >>> s.split()

    ['connect', '123.123.123.123', '21', 'user', 'password']

    btw, the cmd module might be useful for your project:

    http://effbot.org/librarybook/cmd.htm
    http://docs.python.org/lib/module-cmd.html

    </F>
    Fredrik Lundh, Jan 30, 2005
    #3
  4. M.E.Farmer Guest

    wrote:
    > Hi,
    > I'm pretty new to Python, to programming overall...so how would I

    make
    > something where the user inputs multiple words in a string - like
    > "connect 123.123.123.123 21 user password" or similar, and then I can
    > split this string up to pass these arguments to a function like
    > ftp_connect(ip, port, user, pw) etc...? I have no idea how to "break"
    > the string up so I can get these out of it..
    >
    >
    > thanks for answers,
    > munin


    Ok well this is pretty basic but it sounds wrong on some level.
    Maybe you should post some code, you will get better responses.
    You could try something like:
    Py>stuff = "connect 123.123.123.123 21 user password"
    Py>parts_list = stuff.split()# can handle other seperators
    Py>print parts_list
    ['connect', '123.123.123.123', '21', 'user', 'password']
    Py>def StrFtp(userinfo):
    .... parts = userinfo.slpit()
    .... funktion, ip, port, usedr, pw = parts
    .... funktion = funktion.lower()
    .... if funktion == 'connect'
    .... return ftp_connect(ip, port, user, pw)
    .... elif funktion == 'other_function_name_here':
    .... return 'your other action here'
    .... else:
    .... return None

    Py>ftpconnect = StrFtp("connect 123.123.123.123 21 user password")

    Also add asserts and error checking all through the code for malformed
    input.
    hth,
    M.E.Farmer
    M.E.Farmer, Jan 30, 2005
    #4
  5. Guest

    Hey,
    that's exactly what I need! Thanks for your help, the others too of
    course :) Didn't expect to get answers so quickly..
    , Jan 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    519
    tdavisjr
    Jun 16, 2006
  2. Freddy

    Turning Strings into Functions

    Freddy, Sep 26, 2003, in forum: Python
    Replies:
    3
    Views:
    363
    Freddy
    Sep 26, 2003
  3. Kirk McDonald
    Replies:
    7
    Views:
    330
    Alex Martelli
    Jul 3, 2006
  4. Uno
    Replies:
    15
    Views:
    622
    Keith Thompson
    Sep 3, 2010
  5. Steve Litt
    Replies:
    8
    Views:
    120
    James Britt
    Dec 31, 2005
Loading...

Share This Page