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.
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):
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.
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?
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])
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]
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.