Troubles turning unicode into float

I am trying to turn my unicode into a float but I dont know why i cant convert it. I have already tried to use .strip() but thats isnt working so I'm not sure what else I can do.

<type 'unicode'>
<type 'float'>
<type 'unicode'>
 

1
<type 'unicode'>
 

1
<type 'unicode'>
0.00025
0.00025
1
<type 'unicode'>
0.00025
0.00025
1
<type 'unicode'>
0.00100
0.00100
1
<type 'unicode'>
0.00500
0.00500
1
<type 'unicode'>
0.00025
0.00025
1
<type 'unicode'>
 

1
<type 'unicode'>
 

<type 'unicode'>
 

<type 'unicode'>
 

<type 'unicode'>
 

<type 'unicode'>
 

<type 'unicode'>
 

<type 'unicode'>
 

Post code, not pictures of code, see Wiki - how to post code on this forum.

It makes it faster/easier for others to test your code and provide solutions. You can edit your post by clicking the edit icon (pencil icon) on the bottom right of your post.

Moving on from that, it appears that some of your values are returning blank strings? Add something to catch when the values are blank strings and either supply a default value or null.

1 Like

An empty string is not valid for conversion to any number.

2 Likes
t_stamp = system.date.now()
part_number = system.tag.readBlocking("[default]T2_Renishaw/RESDATA_Table_Results[0,7]")[0].value

for i in range(15):
    base_path = "[default]T2_Renishaw/RESDATA_Table_Results["
    path = base_path + str(i) + ",0]"
    feature_value = system.tag.readBlocking(str(path))[0].value
    
    path2 = base_path + str(i) + ",2]"
    actual_value = system.tag.readBlocking(str(path2))[0].value
    
    path3 = base_path + str(i) + ",1]"
    type_value = system.tag.readBlocking(str(path3))[0].value
    
    path4 = base_path + str(i) + ",4]"
    tolerance_value = system.tag.readBlocking(str(path4))[0].value
    
    path5 = base_path + str(i) + ",3]"
    nominal_value = system.tag.readBlocking(str(path5))[0].value
    
    tolerance_value = float(tolerance_value)
    
    if feature_value != " " and actual_value != " " and type_value != " ":
        params2 = {"t_stamp":t_stamp,
                    "part_number":part_number,
                    "feature_name":feature_value,
                    "actual":actual_value,
                    "feature_type":type_value,
                    "tolerance":tolerance_value}
        system.db.runNamedQuery("Renishaw Tops 2 Insert Feature and Actual Values", params2)

its weird because if i put print tolerance_value , there is a value 0.0035. and that is a unicode because when I do print type(tolerance_value) is says unicode

You really shouldn't be using the tolerance_value variable as two different data types. Use a different variable name for either float or string, and see if that shows any other buggy assumptions.

If you put a print on line 25, that's the result you get?

If i'm not mistaken, if you look at my badly edited screenshot, it seems that your fourth and fifth print statements are printing empty lines because the value is empty and the next line is trying to convert nothing into a Float

  • If you replace print with repr(), you can see what your incoming values really are.
  • Assuming it's a blank string what do you want your value to be? Or, should it skip that iteration?
1 Like

oh i see! and yes, if there is a blank value it should skip it

One thing I'm noticing, is this all coming from a dataset tag? If so, there is a lot more efficient way to process this.

You may also want to consider using system.db.runPrepUpdate if you are trying to do a bulk insert.

1 Like

Try this, for performances, but mostly for sanity:

t_stamp = system.date.now()
path = "[default]T2_Renishaw/RESDATA_Table_Results"
data = system.tag.readBlocking([path])[0].value
part_number = data[0][7]

for row in data:
    feature_value = row[0]
    type_value = row[1]
    actual_value = row[2]
    nominal_value = row[3]
    tolerance_value = float(row[4])

    if feature_value != " " and actual_value != " " and type_value != " ":
        params2 = {"t_stamp":t_stamp,
                    "part_number":part_number,
                    "feature_name":feature_value,
                    "actual":actual_value,
                    "feature_type":type_value,
                    "tolerance":tolerance_value}
        system.db.runNamedQuery("Renishaw Tops 2 Insert Feature and Actual Values", params2)

This is strictly equivalent to your inital code, so it won't fix the issue, but it's a saner base to work on.

Is the tag you're reading from a database tag ?
If that's the case, any reason you're not just pulling data from the db directly instead of going through an intermediary tag ?

1 Like