Home > Software design >  store stdout value in python array
store stdout value in python array

Time:02-04

using code :

from subprocess import PIPE, run

command = ['echo', ' 4 3 2 5']
result = run(command, stdout=PIPE, stderr=PIPE,universal_newlines=True)

print(result.stdout)

Now from this result i want to add these values(4,3,2,5) in a python array. need suggestion related this.

Thanks

CodePudding user response:

To create a python list of int (integers) from your output simply use:

numbers = [int(x) for x in result.stdout.strip().split()]

CodePudding user response:

my code attempt :


from subprocess import PIPE, run

command = ['echo', ' 4 3 2 5']
result = run(command, stdout=PIPE, stderr=PIPE,universal_newlines=True)

print(result.stdout)


import numpy as np

arr = np.array([])  # can add .astype(float/int/str ....) be sure append right type

for i in result.stdout.replace(' ','').strip():

    arr = np.append(arr,[i])        # array of strings
    #arr = np.append(arr,[int(i)])  # array of floats, you append
                                    # int to  numpy default float64 array
    
print(arr)

output:

 4 3 2 5

['4' '3' '2' '5']
  •  Tags:  
  • Related