Extract data from a string

Hello,

I’m looking for the most efficient way to extract certain data from a string so I can use it to alter poses.

the string looks somewhat like this: “TRXP00R00000060(001,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,-179246,-3333,8852,057)”
I want to extract:
-179246
-3333
8852
057

What is the best way to extract these numbers and put them in their own variable?
the numbers will change and impact the program so its important to get it right every time.

1 Like

I would look through the UR script manual under the str_ commands. You might be able to do it with str_find and str_sub depending on how consistent the zeros and ones before the value are.

One possibility is to edit what is sending the string to add an identifier before each of the values you want. Something like:

str_var = “TRXP00R00000060(001,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,a-179246,b-3333,c8852,d057)”
first_loc = str_find(str_var, “a”) //finds location of a
end_loc = str_find(str_var, “,”, first_loc) //finds location of first comma after a
var1 = str_sub(str_var, first_loc, end_loc-first_loc) //separates out the first variable.

Hmmm, might be an easier way than this, but should provide a decent starting point. If you can’t add an identifier, maybe try using str_find and str_sub to remove the stuff before the first parenthesis and loop extracting each number up to the next comma. Then discard all the values that are zero or one.

split() is a method in C and Java and most of Polyscope uses things like that. But I’m not near a manual or system to confirm this. it will return an array without the commas as parts of the strings.

-ken c

Where is the string coming from? It looks to be maybe a telegram from a SensoPart camera?

yes, you are right.
Its a String from a sensopart camera.

I got it a working program using str_sub and str_find.

you can also use Sensopart’s script “visor_getAdditionalData(0,0)”.

so something like… to_num(visor_getAdditionalData(1, 8) where 1 is the camera number and 8 is data you added to the telegram. telegrams will always need to be ASCII and some conversions may need to be done for INT or float.

But all detectors have a bunch or extra values you can add to telegram just be sure the first part of the telegram start with detector results and then a pose. then add any additional data you want and use visor_getAdditionalData(0,0) to pull into Polyscope. I will usually just assign these values to a variable either right after taking the image or in a thread.

You can use re.findall in Python to grab those numbers from the string. Here’s a quick example:
import re

s = "TRXP00R00000060(001,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,-179246,-3333,8852,057)"
numbers = re.findall(r"-?\d+", s)
x, y, z, r = map(int, numbers[-4:])  # Grab the last 4 numbers

This gets you the last 4 values every time, no matter how the rest of the string changes. Hope it helps!