使用 zip 时出现类型错误
Typeerror when using zip
我正在尝试使用我的 python 脚本将多个 xlsx 文件中的特定行添加到列表中。我要添加的行是第 4 列(E 列)的单元格值减去第 1 列(B 列)的单元格值不等于 0 的行。我的 xlsx 文件如下所示:
A B C D E F G H
1 A10 2 A10 2 AB
2 A105 1 A105 2 AB
所以对于下面的代码,我希望将第二行添加到开放列表数字中,因为2-1的总和不为0。然后我想通过将它们添加到列列表中来对它们进行排序,然后将它们放入一个新列表,主列表,其中所有内容都已排序。这是我的代码:
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
这里是错误:
Traceback:
for col1, col2, col4, col5, col7, col8 in zip
TypeError: 'type' object is not iterable.
这给我错误。
我知道这里有一些错误,很抱歉,但这是我能想到的解决任务的最佳方法。任何人都可以帮助我吗?我将不胜感激!我是 python 的新手。在 Python 3.4.1.
中工作
您的问题是您对 zip
的使用,这是一个您从未定义的变量。但是,因为 zip()
是一个 built-in function which returns a zip-class
object,所以这件事很混乱。
行 for col1, col2, col4, col5, col7, col8 in zip:
试图找到一个名为 zip
的具有 6 个子组件的迭代器。因为 zip()
是内置的,所以 Python 将此行读取为 "iterate through the zip
type" 但类型不可迭代,因此您会收到相应的错误。如果你选择了不是内置的东西,你会得到 NameError
您的示例有点不清楚,但我相信您可以使用下面的 finally
块 (proof of concept):
来修复它
finally:
columns = zip(col1, col2, col4, col5, col7, col8)
for column in columns:
mainlist.append(column)
我正在尝试使用我的 python 脚本将多个 xlsx 文件中的特定行添加到列表中。我要添加的行是第 4 列(E 列)的单元格值减去第 1 列(B 列)的单元格值不等于 0 的行。我的 xlsx 文件如下所示:
A B C D E F G H
1 A10 2 A10 2 AB
2 A105 1 A105 2 AB
所以对于下面的代码,我希望将第二行添加到开放列表数字中,因为2-1的总和不为0。然后我想通过将它们添加到列列表中来对它们进行排序,然后将它们放入一个新列表,主列表,其中所有内容都已排序。这是我的代码:
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
这里是错误:
Traceback:
for col1, col2, col4, col5, col7, col8 in zip
TypeError: 'type' object is not iterable.
这给我错误。 我知道这里有一些错误,很抱歉,但这是我能想到的解决任务的最佳方法。任何人都可以帮助我吗?我将不胜感激!我是 python 的新手。在 Python 3.4.1.
中工作您的问题是您对 zip
的使用,这是一个您从未定义的变量。但是,因为 zip()
是一个 built-in function which returns a zip-class
object,所以这件事很混乱。
行 for col1, col2, col4, col5, col7, col8 in zip:
试图找到一个名为 zip
的具有 6 个子组件的迭代器。因为 zip()
是内置的,所以 Python 将此行读取为 "iterate through the zip
type" 但类型不可迭代,因此您会收到相应的错误。如果你选择了不是内置的东西,你会得到 NameError
您的示例有点不清楚,但我相信您可以使用下面的 finally
块 (proof of concept):
finally:
columns = zip(col1, col2, col4, col5, col7, col8)
for column in columns:
mainlist.append(column)