Pexe
Pexe

Reputation: 91

Typeerror when using zip

I am trying to add specific rows from several xlsx files to lists with my python script. The rows I am trying to add are the rows where cell values of column number 4 (E column) minus cell values of column number 1 (B column) are not equal to 0. My xlsx files look like this:

   A    B    C    D    E    F    G    H
1 A10   2        A10   2             AB
2 A105  1        A105  2             AB    

So for the following code I want the second row to be added to the open list numbers, because the sum of 2-1 is not 0. Then I want to sort them by adding them to column lists, and then put them into a new list, mainlist, where everything is sorted. Here is my code:

import logging
import pandas as pd
from openpyxl import Workbook, load_workbook
import glob
from openpyxl.utils.dataframe import dataframe_to_rows

numbers = []
rapp = r"C:\Myfolder"
files = glob.glob(rapp)
for file in files:
    df = pd.read_excel(file)
    numbers = df.iloc[:, 4], df.iloc[:,1][df.iloc[:, 4] - df.iloc[:,1] != 0].tolist()
excel_input = load_workbook(excelfile)
ws = excel_input.active
for r in dataframe_to_rows(df, index=True, header=True):
    ws.append(r)
else:
    pass

col1 = []
col2 = []
col4 = []
col5 = []
col7 = []
col8 = []

mainlist = []
try:
    for row in numbers:
        col1.append(ws.cell(row=row, column=1).value)
        col2.append(ws.cell(row=row, column=2).value)
        col4.append(ws.cell(row=row, column=4).value)
        col5.append(ws.cell(row=row, column=5).value)
        col7.append(ws.cell(row=row, column=7).value)
        col8.append(ws.cell(row=row, column=8).value)
except AttributeError:
    logging.error('Something is wrong')
finally:
    for col1, col2, col4, col5, col7, col8 in zip: #Error
        mainlist.append(col1, col2, col4, col5, col7, col8)
return mainlist

Here is the error:

Traceback:
    for col1, col2, col4, col5, col7, col8 in zip 
TypeError: 'type' object is not iterable.

This is giving me errors. I know there are some mistakes here, and I am sorry, but this was the best that I could come up with to solve my task. Can anyone help me on my way? I would be very greatful! I am new to python. Working in Python 3.4.1.

Upvotes: 1

Views: 114

Answers (1)

wnnmaw
wnnmaw

Reputation: 5524

You're issue is your use of zip, which is a variable you never define. However, because zip() is a built-in function which returns a zip-class object, this is confusing the matter.

The line for col1, col2, col4, col5, col7, col8 in zip: is trying to find an iterator called zip with 6 subcomponents. Because zip() is a built-in, Python reads this line as "iterate through the zip type" but types are not iterable, so you receive the corresponding error. Had you picked something which was not a built-in, you would get a NameError

Your example is a little unclear, but I believe you can fix it by using the finally block below (proof of concept):

finally:
    columns = zip(col1, col2, col4, col5, col7, col8)
    for column in columns: 
        mainlist.append(column)

Upvotes: 1

Related Questions