I am trying to parse a string and fill an array with specific information contained in the string, but I am getting some unexpected behavior.
I have written a script that successfully does this for some use cases, but it doesn't work for all possible cases.
Consider the string: 'BEST POSITION:P(0) = 1.124 P(1) = 2.345 P(2) = 3.145 P(3) = 4.354'
The following code should create the list: [1.124, 2.345, 3.145, 4.354]
inputs_best = np.zeros(4)
string_in = 'BEST POSITION:P(0) = 1.124 P(1) = 2.345 P(2) = 3.145 P(3) = 4.354'
best_sols_clean = ''
for item in string_in:
best_sols_clean += item
best_sols_clean = re.sub('[ \t]', '', best_sols_clean)
count = 0
while best_sols_clean.find('P(') is not -1:
line_index = best_sols_clean.find('P(')
try:
inputs_best[count] = float(best_sols_clean[line_index+5:line_index+10])
best_sols_clean = best_sols_clean[line_index+10:-1]
count += 1
except ValueError:
inputs_best[count] = float(best_sols_clean[line_index+5:line_index+6])
best_sols_clean = best_sols_clean[line_index+6:-1]
count += 1
print(inputs_best)
The output of this script is:
[1.124 2.345 3.145 4. ]
For this string, this works, except for the last entry in the list that is cut off at too few digits.
The Except clause is used to catch exceptions when one or more of the values are integers, such as:
string_in = 'BEST POSITION:P(0) = 1 P(1) = 2.345 P(2) = 3.145 P(3) = 4'
which results in an error.
I believe the problem lies with the line best_sols_clean = best_sols_clean[line_index+10:-1] that for some reason throws away trailing digits of the string, even though I am slicing to the last element of the string.
For the string string_in = 'BEST POSITION:P(0) = 1 P(1) = 2.345 P(2) = 3.145 P(3) = 4' the program quits with the error
Traceback (most recent call last):
File "test.py", line 17, in <module>
inputs_best[count] = float(best_sols_clean[line_index+5:line_index+10])
ValueError: could not convert string to float:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 21, in <module>
inputs_best[count] = float(best_sols_clean[line_index+5:line_index+6])
ValueError: could not convert string to float:
I would also be open for a more elegent solution than what I am attempting.
P\(\d\)\s+=\s+([0-9\.]+)which gives 4 matches for your string, where the group 1 is the float.string_inbut I can't edit it since that is just 1 character. You may want to fix that.