[Python] How do I use text from a file as a variable name?

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    How do I use text from a file as a variable name? I am pulling values out of an excel file. I am using xlrd and xlutils with python 3.

    class employee(object):
    def __init__(self, name):
    self.name = name
    emp_list.append(name)
    def bulk_hours(self,sunday=0,monday=0,tuesday=0,wednesday=0,thursday=0,friday=0,saturday=0):
    self.sunday = sunday
    self.monday = monday
    self.tuesday = tuesday
    self.wednesday = wednesday
    self.thursday = thursday
    self.friday = friday
    self.saturday = saturday


    I'm pulling employees out of a spreadsheet. I'm trying to use their actual names. I would love to know any working solution. Thanks!

    Edit: Pardon my ignorance regarding programming and my horrible post. I'm trying to make a simple program that allows me to load an employees name and work schedule from Excel. I will also make sure any edits are saved back into the spreadsheet. The employees are labeled by their names. I'm trying to load their name as a variable so I can do:

    John = employee('John')
    John.bulk_hours(0,8,8,8,8,8,0)
    Stacy = employee('Stacy')
    print(John.monday)


    I'm aiming to use their name as the variable I can use dot notation on. Is this feasible? Is their some other way I should approach this?

    def load(row):
    employee2 = employee(s.cell(row, 0).value)
    employee2.bulk_hours(s.cell(row, 1).value, s.cell(row, 2).value, s.cell(row, 3).value, s.cell(row, 4).value,
    s.cell(row, 5).value, s.cell(row, 6).value, s.cell(row, 7).value)
    print(employee2.saturday)


    I'm trying to use a function like this to load multiple employees and their hours.

    Could I use a list like this somehow?

    worker = ['Joe']
    worker[0] = employee('Joe')
    worker[0].bulk_hours(0,8,8,8,8,8,0)
    print(worker[0].monday)


    Thank you for your valuable time.

    Continue reading...

Compartilhe esta Página