Error while converting string to float

Hi!

I have a script that receives as input a list of values extracted from Excel. Those values are strings. I need to use the unit conversion methods in order to use them. However, i am getting this error as shown on the image below.

The float() function might be causing this, but i tried lots of different ways for converting the input list of strings and nothing seems to work.

Can someone help me?

Thanks in advance

f = float(string) doesn’t do the job?

That specific error doesn’t seem to match up with the line shown in your code. It is implying that the object you are trying to iterate is not an iterable. In other words, if you have a list like this:

nums = [1, 2, 3, 4]

and you try to iterate using a for loop too many times:

for num in nums:
    for n in num: # Will raise a TypeError

The second line is essentially identical to writing this (for the first iteration of the outer forloop):

for n in 1:

2 Likes

Might also be one too few for loops - hard to confirm at the moment t as my phone doesn’t have a large enough screen to see the loops and the list structure at the same time.

1 Like

Actually in my test output i only have a single record in the third loop
(for vs in spacing[2]) which makes your statement @cgartland totally true. However, i need to make this code dinamically, because spacing[2] sometimes can be a list of more then one or two elements, that’s why i am iterating too many times. What can be a solution in that case?

If spacing[2] is either an integer or list of integers, you can use the isinstance function to test whether something is a list or not. For example:

data = [
    [0, 1, 2], # list
    [3, 4, 5], # list
    6 # int
]
for row in data:
    if isinstance(row, list):
        for entry in row:
           # Etc.
    else:
        # Do not iterate row
1 Like

introducing… recursive function~ doesnt matter what kind of list structure you feed, it will just convert anything you feed into a float.

def RecursiveConversion(listofstrings):
	result = list()
	if hasattr(listofstrings, '__iter__'):
		for string in listofstrings:
			result.append(RecursiveConversion(string))
	else:
		result = float(listofstrings)
	return result

OUT = RecursiveConversion(IN[0])
3 Likes

Thanks for that! However i am getting another error:
212

Changed the code a bit:

It doesn’t look like you’re using a recursive function here, which again brings you back to your first error where you’re trying to index a number. If you write something like this, you should be able to narrow down the source of your error:

outlist = []
for sname, col in zip(input_size, collector):
    outlist.append(sname[1][1])
OUT = outlist

If your output is a list of numbers, then it confirms the error since neither ints nor floats can be iterated. For example, the below code is invalid:

nums = [1.000]
for num in nums:
    # num is 1.000
    # Attempting to index 1.000 is invalid
    print num[0]
1 Like

Actually i am using in the node before that one. Before the values were all strings and then in this node i have already converted. Check it out:

212

Even using your code with the recursive function, i get an error:

Which is very strange, because if i remove the float conversion thing, i get:

It seems that when o try to use float/reverse function it is getting the sname[0] (“18.7x122.7”) instead of the real sname[1][0] or sname[1][1].

You may have to integrate your regular expressions into the recursive function. The error shows that your code is attempting to unsuccessfully convert a string (17.5X53) to a float. As I can’t see the full script, I can’t offer a solution, but I would suggest that you identify the reason why some of your strings aren’t being properly parsed before being cast to a float.