Creating an alias in python for importing excel flat files

40 Views Asked by At

I am using the following code to import a xlsx flat file:

master = pd.read_excel("H:/Python_data/profit_US_07 2023.xlsx",skiprows=[0])

I would like to create an alias called infile_nyc having the value of "profit_US_07 2023.xlsx" so that I can use it to import the file

master = pd.read_excel("H:/Python_data/infle_nyc",skiprows=[0]). 

When I try it, I get:

FileNotFoundError: [Errno 2] No such file or directory: 'H:/Python_data/Nyc_infile'. It does not recognize the string.

Thank you for your help

2

There are 2 best solutions below

0
Hyakkimaru On

It seems like there might be a typo in your code. You mentioned that you want to create an alias called "infile_nyc" with the value "profit_US_07 2023.xlsx," but it appears that the alias you're using in the pd.read_excel function is different from what you intended.

To create an alias and use it to import the file correctly, you should do the following:

# Define the alias
infile_nyc = "H:/Python_data/profit_US_07 2023.xlsx"

# Use the alias to read the Excel file
master = pd.read_excel(infile_nyc, skiprows=[0])

In this code, you first define the alias infile_nyc with the correct file path and filename. Then, you use this alias in the pd.read_excel function to read the Excel file. This should resolve the issue, and Python will correctly recognize the string you intended to use as the file path and name.

0
Yosef.Schwartz On

You should use this variable value.

You can use f-strings like below:

master = pd.read_excel(f"H:/Python_data/{infle_nyc}",skiprows=[0])