Disassembling strings and turning them into function parameters

M.E.Farmer mefjr75 at hotmail.com
Sun Jan 30 19:32:55 CET 2005

mercuryp... at gmail.com wrote:
> Hi,
> I'm pretty new to Python, to programming overall...so how would I
> something where the user inputs multiple words in a string - like
> "connect 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 21 user password"
Py>parts_list = stuff.split()# can handle other seperators
Py>print parts_list
['connect', '', '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 21 user password")

Also add asserts and error checking all through the code for malformed

More information about the Python-list mailing list