我试着在谷歌上寻找答案,但我不确定我的措辞是否正确,因为我还是一个初学者,这是我的第一个项目
我有一个代码,可以对excel文档和它的工作表进行排序,并通过分隔的工作表按过道创建新文档。这对我来说很好,尽管有很多重复的代码行。但我希望能够有更多的灵活性(添加或删除它整理出来的过道的数量)
这是我所拥有的东西的一个片段,但它持续了23个通道。我想把它分类
import pandas as pd
import openpyxl
from openpyxl import load_workbook
import xlsxwriter
saveFolder = '/home/Work/Splitting/Aisles/'
aisles = pd.ExcelFile('Split_All.xlsx')
disco = pd.read_excel(aisles, 'Disco')
closed = pd.read_excel(aisles, 'Closed')
oi = pd.read_excel(aisles, 'OI+')
dropship = pd.read_excel(aisles, 'Dropship')
trueouts = pd.read_excel(aisles, 'True Outs')
largemiss = pd.read_excel(aisles, 'Large Missing')
twomiss = pd.read_excel(aisles, 'Two Missing')
onemiss = pd.read_excel(aisles, 'One Missing')
writer=pd.ExcelWriter(saveFolder+'Aisle 01.xlsx', engine='xlsxwriter')
disco2 = disco[disco['Loc'].map(lambda x: x.startswith('01'))]
closed2 = closed[closed['Loc'].map(lambda x: x.startswith('01'))]
oi2 = oi[oi['Loc'].map(lambda x: x.startswith('01'))]
dropship2 = dropship[dropship['Loc'].map(lambda x: x.startswith('01'))]
trueouts2 = trueouts[trueouts['Loc'].map(lambda x: x.startswith('01'))]
largemiss2 = largemiss[largemiss['Loc'].map(lambda x: x.startswith('01'))]
twomiss2 = twomiss[twomiss['Loc'].map(lambda x: x.startswith('01'))]
onemiss2 = onemiss[onemiss['Loc'].map(lambda x: x.startswith('01'))]
disco2.to_excel(writer, sheet_name='Disco', index=False)
closed2.to_excel(writer, sheet_name='Closed', index=False)
oi2.to_excel(writer, sheet_name='OI+', index=False)
dropship2.to_excel(writer, sheet_name='Dropship', index=False)
trueouts2.to_excel(writer, sheet_name='True Outs', index=False)
largemiss2.to_excel(writer, sheet_name='Large Missing', index=False)
twomiss2.to_excel(writer, sheet_name='Missing Two', index=False)
onemiss2.to_excel(writer, sheet_name='Missing One', index=False)
writer.save()
writer=pd.ExcelWriter(saveFolder+'Aisle 03.xlsx', engine='xlsxwriter')
disco4 = disco[disco['Loc'].map(lambda x: x.startswith('03'))]
closed4 = closed[closed['Loc'].map(lambda x: x.startswith('03'))]
oi4 = oi[oi['Loc'].map(lambda x: x.startswith('03'))]
dropship4 = dropship[dropship['Loc'].map(lambda x: x.startswith('03'))]
trueouts4 = trueouts[trueouts['Loc'].map(lambda x: x.startswith('03'))]
largemiss4 = largemiss[largemiss['Loc'].map(lambda x: x.startswith('03'))]
twomiss4 = twomiss[twomiss['Loc'].map(lambda x: x.startswith('03'))]
onemiss4 = onemiss[onemiss['Loc'].map(lambda x: x.startswith('03'))]
disco4.to_excel(writer, sheet_name='Disco', index=False)
closed4.to_excel(writer, sheet_name='Closed', index=False)
oi4.to_excel(writer, sheet_name='OI+', index=False)
dropship4.to_excel(writer, sheet_name='Dropship', index=False)
trueouts4.to_excel(writer, sheet_name='True Outs', index=False)
largemiss4.to_excel(writer, sheet_name='Large Missing', index=False)
twomiss4.to_excel(writer, sheet_name='Missing Two', index=False)
onemiss4.to_excel(writer, sheet_name='Missing One', index=False)
writer.save()
writer=pd.ExcelWriter(saveFolder+'Aisle 04.xlsx', engine='xlsxwriter')
disco2 = disco[disco['Loc'].map(lambda x: x.startswith('04'))]
closed2 = closed[closed['Loc'].map(lambda x: x.startswith('04'))]
oi2 = oi[oi['Loc'].map(lambda x: x.startswith('04'))]
dropship2 = dropship[dropship['Loc'].map(lambda x: x.startswith('04'))]
trueouts2 = trueouts[trueouts['Loc'].map(lambda x: x.startswith('04'))]
largemiss2 = largemiss[largemiss['Loc'].map(lambda x: x.startswith('04'))]
twomiss2 = twomiss[twomiss['Loc'].map(lambda x: x.startswith('04'))]
onemiss2 = onemiss[onemiss['Loc'].map(lambda x: x.startswith('04'))]
disco2.to_excel(writer, sheet_name='Disco', index=False)
closed2.to_excel(writer, sheet_name='Closed', index=False)
oi2.to_excel(writer, sheet_name='OI+', index=False)
dropship2.to_excel(writer, sheet_name='Dropship', index=False)
trueouts2.to_excel(writer, sheet_name='True Outs', index=False)
largemiss2.to_excel(writer, sheet_name='Large Missing', index=False)
twomiss2.to_excel(writer, sheet_name='Missing Two', index=False)
onemiss2.to_excel(writer, sheet_name='Missing One', index=False)
writer.save()
我相当肯定这可以被清理,并减少它使用的线路数量,但这是我为让它为我工作所做的
我认为答案是循环,但我发现的大多数循环信息只是告诉我如何循环列表或字符串,其中我希望根据用户输入使用不同的通道循环以下内容(用户表示他们有28个通道,所以它循环28次,而不是我有23个):
`writer=pd.ExcelWriter(saveFolder+'Aisle 04.xlsx', engine='xlsxwriter')
disco2 = disco[disco['Loc'].map(lambda x: x.startswith('04'))]
closed2 = closed[closed['Loc'].map(lambda x: x.startswith('04'))]
oi2 = oi[oi['Loc'].map(lambda x: x.startswith('04'))]
dropship2 = dropship[dropship['Loc'].map(lambda x: x.startswith('04'))]
trueouts2 = trueouts[trueouts['Loc'].map(lambda x: x.startswith('04'))]
largemiss2 = largemiss[largemiss['Loc'].map(lambda x: x.startswith('04'))]
twomiss2 = twomiss[twomiss['Loc'].map(lambda x: x.startswith('04'))]
onemiss2 = onemiss[onemiss['Loc'].map(lambda x: x.startswith('04'))]
disco2.to_excel(writer, sheet_name='Disco', index=False)
closed2.to_excel(writer, sheet_name='Closed', index=False)
oi2.to_excel(writer, sheet_name='OI+', index=False)
dropship2.to_excel(writer, sheet_name='Dropship', index=False)
trueouts2.to_excel(writer, sheet_name='True Outs', index=False)
largemiss2.to_excel(writer, sheet_name='Large Missing', index=False)
twomiss2.to_excel(writer, sheet_name='Missing Two', index=False)
onemiss2.to_excel(writer, sheet_name='Missing One', index=False)
writer.save()`
这段代码中有两个明显的重复模式,因此它们都可以被
for
循环所取代。还可以使用输入数据帧字典(以工作表名称为键)替换用于这些数据帧的不同变量。您似乎可以执行以下操作,但如果没有输入数据,这是我无法测试的:lambda
函数可以在sheets
上的循环之外定义,但它不是很重要相关问题 更多 >
编程相关推荐