【问题标题】:How to import an excel sheet by taking the file location as input (using Pandas)如何通过将文件位置作为输入来导入 Excel 工作表(使用 Pandas)
【发布时间】:2020-03-07 08:33:49
【问题描述】:

我正在努力学习熊猫。我通常使用以下方法导入 excel 表:

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

但我希望让用户自己输入文件位置。 我尝试将文件位置作为输入并使用格式选项,但它并没有真正为我工作。这是我尝试过的:

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 ")))
     
我只是在这里进行试运行,看看它是否有效。 这是我得到的错误: [Errno 22] 无效参数:'\u202aC:\Users\Ashu\Documents\Book1.xlsx'

请告诉我如何完成这项任务。谢谢你:)

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    以下是正在发生的一些信息: Remove u202a from Python string

    您可以执行以下操作:

    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))
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 2012-07-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      相关资源
      最近更新 更多