我已经在论坛上搜索了几次,而且我还没有找到任何可以在迭代所有行时查找特定列信息的内容。
我有一个包含8列和2200行的电子表格。
我只需要列0 5 8 9(A G J K)
我当前的代码在下面,它返回A-K中的所有值并删除无值。
from openpyxl import Workbook , load_workbook
from itertools import chain
wb = Workbook()
wb = load_workbook("CR_Master_List.xlsx", read_only=True)
ws = wb.active
mylist = []
def iter_rows(ws):
for row in ws.iter_rows():
yield [cell.value for cell in row if cell.value is not None]
mylist = list(iter_rows(ws))
print(mylist)
任何帮助都将不胜感激。
答案 0 :(得分:1)
以下情况应该有效。
cols = [0, 5, 8, 9]
def filter_cols(ws):
for row in ws.iter_rows(max_col=10):
cells = [cell.value for (idx, cell) in enumerate(row) if (
idx in cols and cell.value is not None)]
yield cells