简体   繁体   中英

How to import an excel sheet by taking the file location as input (using Pandas)

I am trying to learn Pandas. I usually import the excel sheet using :

 import pandas as pd df = pd.read_excel (r'C:\\Users\\Comp\\Documents\\filename.xlsx')

But I was hoping to make the user input the file location on their own. I tried taking the file location as the input and using the format option but it didn't really work for me. Here is what I've tried :

 print("Do you have another sheet for a new DF?") ch = input("Press Y or N") if ch =="Y": df2 = pd.read_excel (r'{}'.format(input("copy and paste the location of the excel file here ")))
I was just taking a hit-and-run trial here to see if it works. This is the error that I got : [Errno 22] Invalid argument: '\‪C:\\Users\\Ashu\\Documents\\Book1.xlsx'

Please let me know how I can achieve this task. Thank you :)

Here is some information what is going on: Remove u202a from Python string

You could do the following:

print("Do you have another sheet for a new DF?")
ch = input("Press y or n")

if ch =="y":
    filePath = input("copy and paste the location of the excel file here ")
    filePath = filePath.strip("‪u202a")
    df2 = pd.read_excel (r'{}'.format(filePath))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM