【发布时间】:2014-11-29 22:47:22
【问题描述】:
我正在尝试熟悉 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 here 的建议,但仍然无法正常工作(语法错误)。正如here所建议的那样,我已经按照接受的答案中给出的方式在 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'
我做错了什么?我应该怎么做?
【问题讨论】:
-
发布语法错误文本。
标签: python excel xlrd spyder pythonxy