Invalid Argument in pd.read_excel

狂风中的少年 提交于 2021-01-28 11:25:20

问题


f=[]
for root, dirs, files in os.walk(os.path.abspath(r"F:\Mathnasium Project\Downloaded files")):
    for file in files:
        f.append(os.path.join("r"+'"'+root, file+'"'))


for x in f:
    print(x)
    z=pd.read_excel(x)
    student_report=pd.merge(student_report,z,how='left',left_on='Student Name',right_on='Student')

an error comes up as invalid argument in the pd.read_excel()

OSError: [Errno 22] Invalid argument: 'r"F:\\Mathnasium Project\\Downloaded files\\Abdelrahman Mahmoud LP  05_11_2020.xlsx"'

and i don't know why, any solutions?


回答1:


It looks like something is wrong here: f.append(os.path.join("r"+'"'+root, file+'"'))

Try to print all values of 'f' and check whether it is a valid string or not to be passed to read_excel. like new_file=pd.read_excel(r"C:\Users\a\b\c\Raw Data\CO\acb.xls")




回答2:


It looks like you are trying to build a raw string to deal with backslashes in os.path.join("r"+'"'+root, file+'"'), but it doesn't work that way. The "r" is used when writing string literals like r"F:\Mathnasium Project\Downloaded files" in source files so that python's conversion of the text into a python string is not tripped up by the backslashes.

Once its a python string, there is no raw/not-raw variant. Any escaping is complete. All you really did was create a string

'r"F:\\Mathnasium Project\\Downloaded files\\Abdelrahman Mahmoud LP  05_11_2020.xlsx"'

and a windows path can't literally start with the characters 'r', '"', 'F', ':'. Instead, just join the already properly contructed strings from walk.

f=[]
for root, dirs, files in os.walk(os.path.abspath(r"F:\Mathnasium Project\Downloaded files")):
    for file in files:
        f.append(os.path.join(root, file))


来源:https://stackoverflow.com/questions/64730768/invalid-argument-in-pd-read-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!