简体   繁体   中英

Get a value from columns in the output of Python subprocess.call

Before explaining my question I want to mention that I have taken a look at various other questions at StackOverflow but couldn't find any solution related to my problem. So, that's why don't mark this as duplicate, please!

I'm working on a Python(3.6) project in which I need to run a terminal command and parse a value from the output which is in the form of columns.

Here's the command I ran:

output = subprocess.call('kubectl get svc', shell=True)

And here's the output:

b'NAME         TYPE          CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
kubernetes   ClusterIP      10.35.240.1     <none>          443/TCP          28m
node-app1    LoadBalancer   10.35.245.164   35.239.29.239   8000:32249/TCP   26m

Now, I need to get EXTERNAL-IP from the second row and 4th column.

How can I get this value?

You can extract the specific column from shell itself. This way we can avoid overhead created by text processing.

out = subprocess.check_output(["kubectl get svc | awk '{print $3}'"], shell=True)
result = out.decode().split('\n')
print(result[1])

output:

10.0.0.1

The shell is nice for that. How about

output = subprocess.call('kubectl get svc | tr "\t" " " | tr -s " " | cut -d " " -f 4 | tail -1', shell=True)

You could also omit the tail -1 , which gives the last line, and do that splitting/filtering in Python.

You can parse the output yourself in python as well:

# Step 1, convert the bytes output into string
output = output.decode('utf-8')
# Step 2, split the string based on the newline character
output = output.split('\n')
# Step 3, split all lines on any whitespace character
output = [o.split() for o in output]
# Step 4, get the correct value as [row][column]
value = output[2][3]

You can use pandas to read the data. Here's an selfcontained example:

from StringIO import StringIO   
import pandas

x=b"""NAME         TYPE          CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
kubernetes   ClusterIP      10.35.240.1     <none>          443/TCP          28m
node-app1    LoadBalancer   10.35.245.164   35.239.29.239   8000:32249/TCP   26m"""

dataframe = pandas.read_csv(StringIO(x), sep="\s+")

# print rows  
for index, row in dataframe.iterrows():
   print (row['NAME'], row['CLUSTER-IP'], row['PORT(S)'])

# search for a row with name node-app1 and print value in PORT(S) column:
print dataframe.loc[dataframe['NAME'] == 'node-app1']['PORT(S)'].to_string(index=False)

Using some string manipulation

Demo:

output = b"""NAME         TYPE          CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
kubernetes   ClusterIP      10.35.240.1     <none>          443/TCP          28m
node-app1    LoadBalancer   10.35.245.164   35.239.29.239   8000:32249/TCP   26m"""

output = iter(output.split("\n"))
next(output)     #Skip Header
for i in output:
    print(i.split()[3])   #str.split and get index 3

Output:

<none>
35.239.29.239

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM