2

My code uses pandas to extract information from an excel sheet. I created a function to read and extract what I need and then I set it as two variables so I can work the data.

But the start of my code seems a bit messy. Is there a way to rewrite it?

file_locations = 'C:/Users/sheet.xlsx' def process_file(file_locations): file_location = file_locations df = pd.read_excel(fr'{file_location}') wagon_list = df['Wagon'].tolist() weight_list = df['Weight'].tolist() 

It seems stupid to have a variable with the file destination and then set the file_location for pandas inside my function as the variable. I'm not sure if I could use file_location as the variable outside and inside the function I would call file_location = file_location.

Thanks for any input!

2
  • 1
    def process_file(file_location): ? Commented Oct 12, 2022 at 20:42
  • Well you can use a function without parameter and use the external variable inside it. This however makes your function dependent on this external variable. Commented Oct 12, 2022 at 20:42

1 Answer 1

1

You can simply remove the setting of the file location inside the function.

file_location = 'C:/Users/sheet.xlsx' def process_file(): df = pd.read_excel(fr'{file_location}) wagon_list = df['Wagon'].tolist() weight_list = df['Weight'].tolist() 

But it depends on what you are trying to do with the function as well. Are you using the same function with multiple files in different locations? or is it the same file over and over again.

If it's the later then this seems fine.

You could instead do something like this and feed the location string directly into the function. This is more of a "proper" way to do things.

def process_file(file_location): df = pd.read_excel(file_location) wagon_list = df['Wagon'].tolist() weight_list = df['Weight'].tolist() process_file('C:/Users/sheet.xlsx') 
Sign up to request clarification or add additional context in comments.

3 Comments

In my opinion the second one - with the parameter - is the way to go. Functions should get everything they need via parameter(s) and give back the result with return.
fr'{file_location}' can simply be: file_location
I'll go with the second option. Thanks alot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.