I’m a python novice and am struggling with some concepts here – any help is appreciated.
I’ve got a custom system tool that queries a database, and returns several lines as results to be read — one on each line. The following python script accepts the site FQDN from raw_input and runs $path on that fqdn.
#!/usr/bin/python
import subprocess
import getpass
#get the site name.
site = raw_input("What is the name of the site?: ").strip()
#run path.
cmd = 'path '+ site;
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE);
path_output = p.stdout.read().strip().split('\n')
print path_output
Which returns results like this this:
[' fqdn = www.hcasc.info', ' account_id = 525925', ' parent_id = 525925', ' nfs = /mnt/stor7-wc2-dfw1/525925/www.hcasc.info', ' server_type = PHP5', ' ssl = False', ' host_ip = 98.129.229.186', ' cgi_hosting = False', ' test_link_ip = 98.129.229.186', ' ipv6_ip = 2001:4800:7b02:100::1600:0']
How can I get that extra whitespace out from the “nfs = etc”, or just take the third column (aka awk ‘{print $3}’) and/or assign each piece of these results from bash to separate variables for further manipulation?
Just having some trouble mounting this learning curve, your help is sincerely appreciated.
The third column would be
line.split()[2]; if you want to throw out the first two words and take the rest, it’sline.split(None, 2)[-1]. (splitwith no arguments, or aNoneas the first argument, splits on any whitespace.)Explanation:
(l.split(None, 2) for l in path_output)is a generator expression, which runsl.split(None, 2)for each value ofpath_output(calling itl). It’s like a list comprehension, which is the same thing but with[]around it instead of(), but it only runs thel.splitcall as theforloop passes over it and then forgets the previous values, whereas the list comprehension would construct one big list with all the results ofl.splitat each step first, and then loop over that list normally. This way is like doingbut a little shorter. 🙂
If you want to put this into a dictionary, as DSM suggests, you could do this in this way (just for context) as
or, in Python 2.7 / 3, the much nicer
Of course, you could make this a little more readable on two lines:
Whether you want a dictionary or just a loop depends on what processing you’re going to be doing with it, but the dictionary is probably a better approach for most things.