I have 5 nested for
loops below, to add rows of data from multiple files to one of two lists. Is there a more pythonic way of doing this? I've come across an iterator-generator method named iteritems()
-- could this be used to make this code more pythonic?
# 5 nested loops
for root,dirs,files in os.walk(src):
files = [ _ for _ in files if _.endswith('.xlsx') ]
for file in files:
wb = xlrd.open_workbook(os.path.join(root,file))
worksheets = wb.sheet_names()
for worksheet_name in worksheets:
if worksheet_name.rfind('7600') != -1 :
sheet = wb.sheet_by_name(worksheet_name)
keys = [sheet.cell(3, col_index).value for col_index in xrange(sheet.ncols)]
for row_index in xrange(4, sheet.nrows):
d = {keys[col_index]: sheet.cell(row_index, col_index).value
for col_index in xrange(sheet.ncols)}
if file.rfind('oam') != -1 :
list_7600EoX.append(d)
else:
list_7600EoX_OAM.append(d)
glob
instead of the outer two loops. And you can avoid the extra indentation of that first conditional by inverting the logic:if worksheet_name.rfind('7600') == -1: continue
– wim May 25 at 13:24glob
really be told to go through all subdirectories? How? – Stefan Pochmann May 25 at 13:48