motaha
motaha

Reputation: 403

subprocess.check_output logical error

I am running Xyce (SPICE simulator) simulation using subprocess.check_output because I want use its results for further analysis.

Here is the code I am using:

x=subprocess.check_output(['./Xyce','Circuit.cir'])
volt=[]
for i in range(1,4): 
    start=x.find('FINAL_COL{}_VOLT = ' .format(i)) + 18

    end=x.find('Measure Start Time')

    volt.append(x[start:end])
print colored ('volt=','cyan')  

Here is the log I am getting for the Xyce simulation:

FINAL_COL1_VOLT = 0.0145203
Measure Start Time= 0   Measure End Time= 1
FINAL_COL2_VOLT = 0.0176678
Measure Start Time= 0   Measure End Time= 1
FINAL_COL3_VOLT = 0.0811186
Measure Start Time= 0   Measure End Time= 1

I am getting the result volt=[' 0.0145203\n','',''] and I was expecting to get volt=[' 0.0145203\n',' 0.0176678\n',' 0.0811186\n']. I did some debugging and I found that the problem is my end because 'Measure Start Time' is repeated after every result. So, when I tried to change my end to another string the code went through, but off course didn't give me the desired output because I am not stopping after I get the VOLT value.

So, an ideas on how I can solve this problem.

Thanks in advance

Upvotes: 2

Views: 40

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

If you want all the volts re might be a better approach:

lines = """FINAL_COL1_VOLT = 0.0145203
Measure Start Time= 0   Measure End Time= 1
FINAL_COL2_VOLT = 0.0176678
Measure Start Time= 0   Measure End Time= 1
FINAL_COL3_VOLT = 0.0811186
Measure Start Time= 0   Measure End Time= 1"""

import re

print(re.findall(r"(?<=_VOLT =\s)\d+\.\d+", lines))
['0.0145203', '0.0176678', '0.0811186']

Or split the lines pulling the lines you want:

print([line.split()[-1] for line in lines.splitlines() if line.startswith("FINAL")])
['0.0145203', '0.0176678', '0.0811186']

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155363

You're not providing offsets for the find of Measure Start Time. Change the second find to:

end=x.find('Measure Start Time', start)

and the search for end will start from the start you identified, not the beginning of the string (which always finds the same end).

Upvotes: 3

Related Questions