【发布时间】:2020-07-01 10:24:43
【问题描述】:
我正在使用 pd.ExcelFile 如下打开和解析文件,但目前只有 actual 文件夹路径和文件名在一个字符串中。
wb = pd.ExcelFile(folder_path+filename)
我想把它放到一个函数中,要求用户提供路径和文件名并处理无效输入。我开始了类似下面的操作,但似乎无论如何都不会在函数内部生成错误,而且我不确定如何说“虽然 wb 不是有效的东西”继续提示输入文件路径,直到我们得到一个有效的路径?
def Load_Parse():
folder_path = input('\nEnter the path to the qry_T spreadsheet here (include slashes at the start and at the end): ')
filename = input('\nEnter the name of the spreadsheet to be used here: ')
sheetname = input('\nEnter the sheet containing the data here, including the extension (e.g. "qry_Trajectory 2019.xlsx": ')
try:
wb = pd.ExcelFile(folder_path+filename)
except FileNotFoundError:
有什么想法吗?
然后我将使用我希望的类似方法解析文件:
df = wb.parse('filename')
【问题讨论】:
标签: python-3.x pandas input while-loop