python导入excel文件,报错

尝试导入excel文件操作,编写了以下代码,

import xlrd
workbook = xlrd.open_workbook('D:\ben.qin\Desktop\1.xls')
worksheets = workbook.sheet_names()
print('worksheets is %s' %worksheets)

报出以下错误,还请帮忙指点错误


File "D:/工具/Python/实例/读取文件.py", line 9, in <module>
    workbook = xlrd.open_workbook('D:\ben.qin\Desktop\1.xls')

  File "D:\工具\Anaconda3\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
    f = open(filename, "rb")

OSError: [Errno 22] Invalid argument: 'D:\x08en.qin\\Desktop\x01.xls'


慕沐林林
浏览 1429回答 2
2回答

慕尼黑的夜晚无繁华

报错原因是方法中的参数为转义字符。open_workbook(r'D:dir')这样写,或者open_workbook('D:\\dir')这样写,或者为open_workbook('D:/dir')这样写

慕妹3146593

open_workbook(r'D:dir')少写一个反斜扛,改为这样的格式open_workbook(r'D:\dir\dir\dir')
打开App,查看更多内容
随时随地看视频慕课网APP