for row in worksheet1.iterrows(minrow=2, mincol=11, maxcol=18, values_only=True): if…
- User
for row in worksheet1.iter_rows(min_row=2, min_col=11, max_col=18, values_only=True):
if any(x in str(row[0]) for x in [“集成电路”, “半导体”]):
添加来自K列的行
worksheetR.append([“K”] + list(row))
if not any(x in str(row[i]) for i, x in enumerate([“集成电路”, “半导体”], start=4)): continue
elif not any(x in str(row[0]) for x in [“集成电路”, “半导体”]) and any(x in str(row[3]) for x in [“集成电路”, “半导体”]):
添加来自L列的行
worksheetR.append([“L”] + list(row))
if not any(x in str(row[i]) for i, x in enumerate([“集成电路”, “半导体”], start=5)): continue elif not any(x in str(row[0:4]) for x in [“集成电路”, “半导体”]) and any(x in str(row[4]) for x in [“集成电路”, “半导体”]):
添加来自M列的行
worksheetR.append([“M”] + list(row))
if not any(x in str(row[i]) for i, x in enumerate([“集成电路”, “半导体”], start=6)): continue elif not any(x in str(row[0:5]) for x in [“集成电路”, “半导体”]) and any(x in str(row[5]) for x in [“集成电路”, “半导体”]):
添加来自N列的行
worksheetR.append([“N”] + list(row))
if not any(x in str(row[i]) for i, x in enumerate([“集成电路”, “半导体”], start=7)): continue elif not any(x in str(row[0:6]) for x in [“集成电路”, “半导体”]) and any(x in str(row[6]) for x in [“集成电路”, “半导体”]):
添加来自O列的行
worksheetR.append([“O”] + list(row))
if not any(x in str(row[i]) for i, x in enumerate([“集成电路”, “半导体”], start=8)): continue elif not any(x in str(row[0:7]) for x in [“集成电路”, “半导体”]) and any(x in str(row[7]) for x in [“集成电路”, “半导体”]):
- Assistant
添加来自P列的行
worksheetR.append([“P”] + list(row))
上述代码的作用是从worksheet1的第二行开始,逐行遍历第11-18列的数据,
如果第一列或第四列中含有“集成电路”或“半导体”这两个关键词,则将该行从K列开始的数据添加到worksheetR中,
如果第一列中不含有这两个关键词,但第四列中含有,则将该行从L列开始的数据添加到worksheetR中,
如果第一列和第四列均不含有这两个关键词,但是第五列中含有,则将该行从M列开始的数据添加到worksheetR中,
以此类推,直到第八列。这样,就将含有“集成电路”或“半导体”这两个关键词的数据根据它们所在的列添加到了worksheetR中。
- User
请对上述代码进行缩进,使其正确运行
- Assistant
for row in worksheet1.iter_rows(min_row=2, min_col=11, max_col=18, values_only=True): if any(x in str(row[0]) for x in ["集成电路", "半导体"]): # 添加来自K列的行 worksheetR.append(["K"] + list(row))
if not any(x in str(row[i]) for i, x in enumerate(["集成电路", "半导体"], start=4)): continue elif not any(x in str(row[0]) for x in ["集成电路", "半导体"]) and any(x in str(row[3]) for x in ["集成电路", "半导体"]): # 添加来自L列的行 worksheetR.append(["L"] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(["集成电路", "半导体"], start=5)): continue elif not any(x in str(row[0:4]) for x in ["集成电路", "半导体"]) and any(x in str(row[4]) for x in ["集成电路", "半导体"]): # 添加来自M列的行 worksheetR.append(["M"] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(["集成电路", "半导体"], start=6)): continue elif not any(x in str(row[0:5]) for x in ["集成电路", "半导体"]) and any(x in str(row[5]) for x in ["集成电路", "半导体"]): # 添加来自N列的行 worksheetR.append(["N"] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(["集成电路", "半导体"], start=7)): continue elif not any(x in str(row[0:6]) for x in ["集成电路", "半导体"]) and any(x in str(row[6]) for x in ["集成电路", "半导体"]): # 添加来自O列的行 worksheetR.append(["O"] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(["集成电路", "半导体"], start=8)): continue elif not any(x in str(row[0:7]) for x in ["集成电路", "半导体"]) and any(x in str(row[7]) for x in ["集成电路", "半导体"]): # 添加来自P列的行 worksheetR.append(["P"] + list(row)) - User
import openpyxl
#打开工作簿和表格 workbook = openpyxl.load_workbook("C:/Users/Administrator/Desktop/集成电路测试文件.xlsx") worksheet1 = workbook['Sheet1']
#新建筛选结果表Sheet(R1) worksheetR = workbook.create_sheet('Sheet(R1)')
for row in worksheet1.iter_rows(min_row=2, min_col=11, max_col=18, values_only=True): if any(x in str(row[0]) for x in ['集成电路', '半导体']):
添加来自K列的行
worksheetR.append(['K'] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=4)): continue elif not any(x in str(row[0]) for x in ['集成电路', '半导体']) and any(x in str(row[3]) for x in ['集成电路', '半导体']):
添加来自L列的行
worksheetR.append(['L'] + list(row))
if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=5)): continue elif not any(x in str(row[0:4]) for x in ['集成电路', '半导体']) and any(x in str(row[4]) for x in ['集成电路', '半导体']):
添加来自M列的行
worksheetR.append(['M'] + list(row))
if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=6)): continue elif not any(x in str(row[0:5]) for x in ['集成电路', '半导体']) and any(x in str(row[5]) for x in ['集成电路', '半导体']):
- Assistant
代码存在缩进错误,需要进行修正后才能正确运行,修正后的代码如下:
import openpyxl
#打开工作簿和表格 workbook = openpyxl.load_workbook("C:/Users/Administrator/Desktop/集成电路测试文件.xlsx") worksheet1 = workbook['Sheet1']
#新建筛选结果表Sheet(R1) worksheetR = workbook.create_sheet('Sheet(R1)')
for row in worksheet1.iter_rows(min_row=2, min_col=11, max_col=18, values_only=True): if any(x in str(row[0]) for x in ['集成电路', '半导体']): # 添加来自K列的行 worksheetR.append(['K'] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=4)): continue elif not any(x in str(row[0]) for x in ['集成电路', '半导体']) and any(x in str(row[3]) for x in ['集成电路', '半导体']): # 添加来自L列的行 worksheetR.append(['L'] + list(row))
- User
Traceback (most recent call last): File "C:/Users/Administrator/Desktop/python作品/对发票数据标记标签/绍兴市项目excel筛选3(集成电路).py", line 29, in if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=8)): File "C:/Users/Administrator/Desktop/python作品/对发票数据标记标签/绍兴市项目excel筛选3(集成电路).py", line 29, in if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=8)): IndexError: tuple index out of range
- Assistant
报错信息显示一个元组下标越界错误,这是因为在第8列之后的数据无法进行遍历与操作,所以要对代码进行一定的修改。一种解决方法是在对列表进行遍历的时候先判断该列表的长度是否大于或等于指定的列数,不足则跳过对该列的遍历。修改后的代码如下:
import openpyxl
#打开工作簿和表格 workbook = openpyxl.load_workbook("C:/Users/Administrator/Desktop/集成电路测试文件.xlsx") worksheet1 = workbook['Sheet1']
#新建筛选结果表Sheet(R1) worksheetR = workbook.create_sheet('Sheet(R1)')
for row in worksheet1.iter_rows(min_row=2, min_col=11, max_col=18, values_only=True): if any(x in str(row[0]) for x in ['集成电路', '半导体']): # 添加来自K列的行 worksheetR.append(['K'] + list(row)) if not any(x in str(row[i]) for i, x in enumerate(['集成电路', '半导体'], start=4)): continue elif not any(x in str(row[0]) for x in ['集成电路', '半导体']) and any(x in str(row[3]) for x in ['集成电路', '半导体']): # 添加来自L列的行 worksheetR.append(['L'] + list(row))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.