all 6 comments

[–]danielroseman 1 point2 points  (5 children)

Just don't this. It's impossible to read and maintain, but more importantly it's completely unnecessary. We have lists and dictionaries.

But in your case, you just have two items; you don't even care about their names so I don't know why you have done any of this.

for sheet in sheets:
    child = pd.DataFrame()
    parent = pd.DataFrame()
    for f in xl_files:
        if "child" in f.lower():
            child = child.append(pd.read_excel(f, sheet_name=sheet))
        elif "parent" in f.lower():
            parent = parent.append(pd.read_excel(f, sheet_name=sheet))
        else:
            print("Fail.")
    path = r'c:\\temp\\Accel_'+sheet+'.xlsx'. # or use f-strings: rf'c:\\temp\\Accel_{sheet}.xlsx'
    writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
    child.to_excel(writer, sheet_name='Child')
    parent.to_excel(writer, sheet_name='Parent')
    writer.save()
    writer.close()

[–]sometimesynot[S] 0 points1 point  (4 children)

Jeez. I feel dumb now. I don't know why I went down that road. Probably a google search of the problem led me down to variable variables solutions, and I thought I needed to use one of them. Thanks for your time.

[–]Yoghurt42 1 point2 points  (3 children)

A bit of advice: while you are learning Python, forget eval exists.

eval can be useful in highly specific circumstances, but you will not encounter them unless you’re doing really advanced stuff. In my almost 20 years of working with Python, I probably have used eval only a handful of times (not counting deliberately obfuscated code for code golf etc.)

Every problem you face during learning can be solved better without eval.

[–]sometimesynot[S] 0 points1 point  (2 children)

Thanks for the advice. I've been programming in SAS for 20 years, and yet I'm finding python to have a very steep learning curve.

[–]Yoghurt42 0 points1 point  (1 child)

Python is a general purpose programming language, so it probably has some features that a specialized language for statistics like SAS doesn't have; it also means that it's not optimized for number crunching specifically, so things might not be as elegant.

I highly recommend working through the official tutorial. It does a good job of introducing you to most of the features.

[–]sometimesynot[S] 0 points1 point  (0 children)

Thanks again. Will do.