我正在尝试熟悉 xlrd,所以我将一个示例复制到我的 IDE(spyder)中。我正在使用 python(x,y) 2.7.6.1
这是我的例子
import xlrd
import os
filename=os.path.join("C:/","Desktop/myfile"):
book = xlrd.open_workbook(filename)
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
sh = book.sheet_by_index(0)
print sh.name, sh.nrows, sh.ncols
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
for rx in range(sh.nrows):
print sh.row(rx)
如您所见,我在这里听取了关于 SE 的建议,但它仍然不起作用(语法错误)。正如这里所建议的那样,我已经以接受的答案中给出的方式在 os.path.join() 中写了一些东西。
这是错误日志:
runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/PC/.spyder2/.temp.py", line 12
filename=os.path.join("C:/","/Users/PC/Desktop/myfile"):
^
SyntaxError:无效的语法
更新现在,当我用“join”从行尾删除冒号时,我得到了另一个语法错误。就是这个:
runfile('C:/Users/PC/.spyder2/.temp.py', wdir=r'C:/Users/PC/.spyder2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/PC/.spyder2/.temp.py", line 13, in <module>
book = xlrd.open_workbook(filename)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook
f = open(filename, "rb")
IOError: [Errno 2] No such file or directory: 'C:/Users/PC/Desktop/myfile'
我究竟做错了什么?我应该怎么做?