TypeError: 'float' object is not iterable

A

Ana Dionísio

Hi!!

I keep having this error and I don't know why: TypeError: 'float' object is not iterable.

I have this piece of code, that imports to python some data from Excel and saves it in a list:

"
t_amb = []

for i in range(sh2.nrows):
t_amb.append(sh2.cell(i,2).value)

print t_amb

"
Here is everything ok.

But then, I need to pass the data again to exel, so I wrote this:

"
a=8
for b in range (len(t_amb)):
a=8
for d in t_amb:
a=a+1
sheet.write(a,b+1,d)
"

The error appear in "for d in t_amb:" and I don't understand why. Can you help me?
 
J

Joel Goldstick

Hi!!

I keep having this error and I don't know why: TypeError: 'float' object
is not iterable.

I have this piece of code, that imports to python some data from Excel and
saves it in a list:

"
t_amb = []

for i in range(sh2.nrows):
t_amb.append(sh2.cell(i,2).value)

print t_amb

"
Here is everything ok.

But then, I need to pass the data again to exel, so I wrote this:

"
a=8
for b in range (len(t_amb)):
a=8
for d in t_amb:
a=a+1
sheet.write(a,b+1,d)
"

The error appear in "for d in t_amb:" and I don't understand why. Can
you help me?


Most likely the value of t_amb[ is a float. It would have to be a list
or a tuple or some other sequence to be iterable. I can't tell what you
are trying to do here
 
A

Ana Dionísio

But isn't t_amb a list? I thought that the first piece of script would create a list.

I'm trying to create a list named t_amb with some values that are in a Excel sheet. And then I need to export that list to another Excel sheet
 
A

Ana Dionísio

But isn't t_amb a list? I thought that the first piece of script would create a list.

I'm trying to create a list named t_amb with some values that are in a Excel sheet. And then I need to export that list to another Excel sheet
 
M

MRAB

Hi!!

I keep having this error and I don't know why: TypeError: 'float' object is not iterable.

I have this piece of code, that imports to python some data from Excel and saves it in a list:

"
t_amb = []

for i in range(sh2.nrows):
t_amb.append(sh2.cell(i,2).value)

print t_amb

"
Here is everything ok.

But then, I need to pass the data again to exel, so I wrote this:

"
a=8
for b in range (len(t_amb)):
a=8
for d in t_amb:
a=a+1
sheet.write(a,b+1,d)
"

The error appear in "for d in t_amb:" and I don't understand why. Can you help me?

t_amb is a list of float, so t_amb is a float, but you can't iterate
over a float.
 
J

Joel Goldstick

But isn't t_amb a list? I thought that the first piece of script would
create a list.
t_amb might be a list, but t_amb is apparently a number of type float
that is in that list
 
C

Chris Rebert

Hi!!

I keep having this error and I don't know why: TypeError: 'float' object is not iterable.

In general, in the future, always include the full exception
Traceback, not just the final error message. The extra details this
provides can greatly aid debugging.
I have this piece of code, that imports to python some data from Excel and saves it in a list:

"
t_amb = []

for i in range(sh2.nrows):
t_amb.append(sh2.cell(i,2).value)

`t_amb` is a list, and you are apparently putting floats (i.e. real
numbers) into it. `t_amb` is a list of floats.
Therefore every item of `t_amb` (i.e. `t_amb[x]`, for any `x` that's
within the bounds of the list's indices) will be a float.
(Also, you may want to rewrite this as a list comprehension;
http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
)
print t_amb

"
Here is everything ok.

But then, I need to pass the data again to exel, so I wrote this:

"
a=8
This duplicate assignment is pointless.
for b in range (len(t_amb)):
a=8
for d in t_amb:


Given our earlier conclusion, we likewise know that `t_amb` will be
a float (we merely replaced the arbitrary `x` with `b`). A single
float is a scalar, not a collection, so it's nonsensical to try and
iterate over it like you are in "for d in t_amb:"; a number is not
a list. `t_amb` is a lone number, and numbers contain no
items/elements over which to iterate. Perhaps you want just "d =
t_amb" ? Remember that in a `for` loop, the expression after the
`in` (i.e. `t_amb`) is evaluated only once, at the beginning of the
loop, and not repeatedly. In contrast, assuming this were a valid
`for` loop, `d` would take on different values at each iteration of
the loop.

In any case, it's rarely necessary nowadays to manually iterate over
the range of the length of a list; use `enumerate()` instead;
http://docs.python.org/2/library/functions.html#enumerate
a=a+1
sheet.write(a,b+1,d)
"

The error appear in "for d in t_amb:" and I don't understand why. Can you help me?


I hope this explanation has been sufficiently clear. If you haven't
already, you may wish to review the official Python tutorial at
http://docs.python.org/2/tutorial/index.html . You may also find it
helpful to run your program step-by-step in the interactive
interpreter/shell, printing out the values of your variables along the
way so as to understand what your program is doing.

Regards,
Chris
 
J

John Ladasky

for b in range (len(t_amb)):
a=8
for d in t_amb:
a=a+1
sheet.write(a,b+1,d)

The error appear in "for d in t_amb:" and I don't understand why. Can you help me?


It looks to me like you know how to program in some other language, possibly C, and your other language's needs are affecting the way that you write Python.

You are supplying an explicit variable, b to step through... something. I THINK that you want to step through t_amb, and not through the Bth element of t_amb. Python's "in" statement will do this for you automatically, without you having to keep track of an index variable.

You didn't show your import statements, but I assume you are using the xlwtmodule. That's where I find the sheet.write() function. Now, exactly HOWdid you want to write the data back to the Excel file? In a single column? A single row? Or in a diagonal? You have two nested loops. I'm confused by the fact that you are incrementing both the row and column indices for sheet.write().

Do you know about the enumerate() function? It's very handy. It yields a tuple, the first element of which is a number counting up from zero, and the second element of which is an element from the (iterable) variable that you provide.

Does this code accomplish your task?

for column, data in enumerate(t_amb):
sheet.write(8, column+1, data)

Or this?

for row, data in enumerate(t_amb):
sheet.write(row+8, 1, data)

If you have any questions, feel free to ask.
 
D

Dennis Lee Bieber

But isn't t_amb a list? I thought that the first piece of script would create a list.
t_amb likely is a list, yes...

BUT t_amb is ONE ELEMENT OF THE LIST.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top