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


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

Dictionary error

Started byThuruv V <wasp52dex@gmail.com>
First post2014-11-25 15:04 +0530
Last post2014-11-25 16:36 +0000
Articles 3 — 3 participants

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


Contents

  Dictionary error Thuruv V <wasp52dex@gmail.com> - 2014-11-25 15:04 +0530
    Re: Dictionary error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-25 22:30 +1100
    Re: Dictionary error John Gordon <gordon@panix.com> - 2014-11-25 16:36 +0000

#81497 — Dictionary error

FromThuruv V <wasp52dex@gmail.com>
Date2014-11-25 15:04 +0530
SubjectDictionary error
Message-ID<mailman.16275.1416914234.18130.python-list@python.org>

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

Please Clarify the 'TypeError: zip argument #1 must support iteration'


import openpyxl

book = openpyxl.load_workbook('c:/users/c_thv/desktop/tax.xlsx')
sheet = book.get_sheet_by_name('Thilip')
cell = sheet.cell(row=2,column = 4)
i = 2
x = []
y = []while i < 10:
   keys = sheet.cell(row=i,column = 4)
   values = sheet.cell(row = i,column = 5)
   x.append(keys.value)
   y.append(values.value)
   i +=1
   mydict = dict(zip(keys,values)print mydict

[toc] | [next] | [standalone]


#81498

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-11-25 22:30 +1100
Message-ID<54746858$0$13009$c3e8da3$5496439d@news.astraweb.com>
In reply to#81497
Thuruv V wrote:

> Please Clarify the 'TypeError: zip argument #1 must support iteration'

Try it at the interactive interpreter:

py> zip('abc', [1, 2, 3])  # works fine
[('a', 1), ('b', 2), ('c', 3)]


But:

py> zip(1000, [1, 2, 3])  # fails
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip argument #1 must support iteration


So the first argument to zip is not a string or a list, it is something like
an int, or None, or a boolean flag. You'll need to inspect the argument to
see what it is.


> import openpyxl
> 
> book = openpyxl.load_workbook('c:/users/c_thv/desktop/tax.xlsx')
> sheet = book.get_sheet_by_name('Thilip')
> cell = sheet.cell(row=2,column = 4)
> i = 2
> x = []
> y = []while i < 10:
>    keys = sheet.cell(row=i,column = 4)

Insert a line here:

    print keys, type(keys)


and see what it prints.


>    values = sheet.cell(row = i,column = 5)
>    x.append(keys.value)
>    y.append(values.value)
>    i +=1
>    mydict = dict(zip(keys,values)print mydict



-- 
Steven

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


#81520

FromJohn Gordon <gordon@panix.com>
Date2014-11-25 16:36 +0000
Message-ID<m52b5l$koh$1@reader1.panix.com>
In reply to#81497
In <mailman.16275.1416914234.18130.python-list@python.org> Thuruv V <wasp52dex@gmail.com> writes:

> Please Clarify the 'TypeError: zip argument #1 must support iteration'


> import openpyxl

> book = openpyxl.load_workbook('c:/users/c_thv/desktop/tax.xlsx')
> sheet = book.get_sheet_by_name('Thilip')
> cell = sheet.cell(row=2,column = 4)
> i = 2
> x = []
> y = []while i < 10:
>    keys = sheet.cell(row=i,column = 4)
>    values = sheet.cell(row = i,column = 5)
>    x.append(keys.value)
>    y.append(values.value)
>    i +=1
>    mydict = dict(zip(keys,values)print mydict

The purpose of zip() is to accept two (or more) sequences and return a
single sequence with the corresponding members of the input sequences
paired together.

For example, if you had a list of names and a list of ages, calling zip
on the two lists would produce single list of (name, age) pairs.

But you're calling zip on two single items, not sequences, and you
can't do that.

Glancing over the code it appears that you're saving the cell contents
in two lists, x and y.  Perhaps you meant to call zip on those lists
instead of on keys and values?

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [standalone]


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


csiph-web