-2

I'm trying to import an excel file that has multiple sheets to python by using pd.read_excel. Can I define the file path and the excel file names beforehand? This will make it easier to run the script for the different dataset which has different file paths and excel file names. I tried something like this but didn't work:

#Define file path
file_path ="copied and pasted actual file path here"
#Define raw data excel sheet name
raw_data="copied and paste excel file name here"

#import excel 
import pandas as pd

PL_raw_data = pd.read_excel (r'"file_path"/"raw_data".xlsx',sheet_name='copied and pasted excel sheet name here')

Thanks in advance!

2
  • Does this answer your question? How to insert string into a string as a variable? Commented Nov 30, 2021 at 3:55
  • This is your typical how to use a variable question. Some complexity may arise due to the way you use the path. But, storing it in a variable vs not storing it in a variable shouldn't make a difference. Your problem lies in the way you're pass the string. The r-literal makes it so that everything is raw (and is so the quotes are kept in the string). You want to concatenate strings, or better use f-strings elsewhere and simply reference the variable. Commented Nov 30, 2021 at 3:58

1 Answer 1

0

Try this

#Define file path
file_path ="copied and pasted actual file path here"
#Define raw data excel sheet name
raw_data="copied and paste excel file name here"

#import excel 
import pandas as pd

PL_raw_data = pd.read_excel (file_path + raw_data, sheet_name='copied and pasted excel sheet name here')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.