TypeError: 'float' object is not iterable

John Ladasky john_ladasky at sbcglobal.net
Thu Mar 14 14:11:14 EDT 2013


On Thursday, March 14, 2013 3:12:11 AM UTC-7, Ana Dionísio wrote:

> for b in range (len(t_amb)):
>     a=8
>     for d in t_amb[b]:
>         a=a+1
>         sheet.write(a,b+1,d)
> 
> The error appear in "for d in t_amb[b]:" 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 xlwt module.  That's where I find the sheet.write() function.  Now, exactly HOW did 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.



More information about the Python-list mailing list