Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41219 > unrolled thread

TypeError: 'float' object is not iterable

Started byAna Dionísio <anadionisio257@gmail.com>
First post2013-03-14 03:12 -0700
Last post2013-03-14 11:11 -0700
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  TypeError: 'float' object is not iterable Ana Dionísio <anadionisio257@gmail.com> - 2013-03-14 03:12 -0700
    Re: TypeError: 'float' object is not iterable Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-14 06:31 -0400
      Re: TypeError: 'float' object is not iterable Ana Dionísio <anadionisio257@gmail.com> - 2013-03-14 03:34 -0700
        Re: TypeError: 'float' object is not iterable Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-14 06:44 -0400
        Re: TypeError: 'float' object is not iterable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-03-14 18:09 -0400
      Re: TypeError: 'float' object is not iterable Ana Dionísio <anadionisio257@gmail.com> - 2013-03-14 03:34 -0700
    Re: TypeError: 'float' object is not iterable MRAB <python@mrabarnett.plus.com> - 2013-03-14 10:43 +0000
    Re: TypeError: 'float' object is not iterable Chris Rebert <clp2@rebertia.com> - 2013-03-14 03:50 -0700
    Re: TypeError: 'float' object is not iterable John Ladasky <john_ladasky@sbcglobal.net> - 2013-03-14 11:11 -0700

#41219 — TypeError: 'float' object is not iterable

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-03-14 03:12 -0700
SubjectTypeError: 'float' object is not iterable
Message-ID<3984882c-0144-47ba-af4f-b29877bc4698@googlegroups.com>
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[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?

[toc] | [next] | [standalone]


#41220

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-03-14 06:31 -0400
Message-ID<mailman.3291.1363257065.2939.python-list@python.org>
In reply to#41219

[Multipart message — attachments visible in raw view] — view raw

On Thu, Mar 14, 2013 at 6:12 AM, Ana Dionísio <anadionisio257@gmail.com>wrote:

> 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[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?
>

Most likely the value of t_amb[[b] 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

> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#41221

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-03-14 03:34 -0700
Message-ID<225a4a3e-bb0b-406b-8feb-c845e9ab4c9a@googlegroups.com>
In reply to#41220
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

[toc] | [prev] | [next] | [standalone]


#41224

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-03-14 06:44 -0400
Message-ID<mailman.3295.1363257866.2939.python-list@python.org>
In reply to#41221

[Multipart message — attachments visible in raw view] — view raw

On Thu, Mar 14, 2013 at 6:34 AM, Ana Dionísio <anadionisio257@gmail.com>wrote:

> 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[b] is apparently a number of type float
that is in that 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#41241

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-03-14 18:09 -0400
Message-ID<mailman.3321.1363298981.2939.python-list@python.org>
In reply to#41221
On Thu, 14 Mar 2013 03:34:57 -0700 (PDT), Ana Dionísio
<anadionisio257@gmail.com> declaimed the following in
gmane.comp.python.general:

> 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[b] is ONE ELEMENT OF THE LIST.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#41222

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-03-14 03:34 -0700
Message-ID<mailman.3293.1363257794.2939.python-list@python.org>
In reply to#41220
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

[toc] | [prev] | [next] | [standalone]


#41223

FromMRAB <python@mrabarnett.plus.com>
Date2013-03-14 10:43 +0000
Message-ID<mailman.3294.1363257819.2939.python-list@python.org>
In reply to#41219
On 14/03/2013 10:12, Ana Dionísio wrote:
> 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[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?
>
t_amb is a list of float, so t_amb[b] is a float, but you can't iterate
over a float.

[toc] | [prev] | [next] | [standalone]


#41226

FromChris Rebert <clp2@rebertia.com>
Date2013-03-14 03:50 -0700
Message-ID<mailman.3296.1363258242.2939.python-list@python.org>
In reply to#41219
On Thu, Mar 14, 2013 at 3:12 AM, Ana Dionísio <anadionisio257@gmail.com> wrote:
> 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[b]:

Given our earlier conclusion, we likewise know that `t_amb[b]` 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[b]:"; a number is not
a list. `t_amb[b]` is a lone number, and numbers contain no
items/elements over which to iterate. Perhaps you want just "d =
t_amb[b]" ? Remember that in a `for` loop, the expression after the
`in` (i.e. `t_amb[b]`) 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[b]:" 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

[toc] | [prev] | [next] | [standalone]


#41236

FromJohn Ladasky <john_ladasky@sbcglobal.net>
Date2013-03-14 11:11 -0700
Message-ID<5b49a5b4-dfaa-44fe-a60a-8e9aafde9f60@googlegroups.com>
In reply to#41219
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web