Disassembling strings and turning them into function parameters
mefjr75 at hotmail.com
Sun Jan 30 19:32:55 CET 2005
mercuryp... at gmail.com wrote:
> 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 184.108.40.206 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,
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 220.127.116.11 21 user password"
Py>parts_list = stuff.split()# can handle other seperators
['connect', '18.104.22.168', '21', 'user', 'password']
... 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'
... return None
Py>ftpconnect = StrFtp("connect 22.214.171.124 21 user password")
Also add asserts and error checking all through the code for malformed
More information about the Python-list