Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xlrd python不起作用-“;语法错误“;_Python_Excel_Xlrd_Spyder_Pythonxy - Fatal编程技术网

xlrd python不起作用-“;语法错误“;

xlrd python不起作用-“;语法错误“;,python,excel,xlrd,spyder,pythonxy,Python,Excel,Xlrd,Spyder,Pythonxy,我试图熟悉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):",

我试图熟悉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"):
                                                      ^
runfile('C:/Users/PC/.spyder2/.temp.py',wdir=r'C:/Users/PC/.spyder2')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第540行,在runfile中
execfile(文件名、命名空间)
文件“C:/Users/PC/.spyder2/.temp.py”,第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'
runfile('C:/Users/PC/.spyder2/.temp.py',wdir=r'C:/Users/PC/.spyder2')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第540行,在runfile中
execfile(文件名、命名空间)
文件“C:/Users/PC/.spyder2/.temp.py”,第13行,在
book=xlrd.open_工作簿(文件名)
文件“C:\Python27\lib\site packages\xlrd\\uuuuu init\uuuuuu.py”,第394行,在OpenU工作簿中
f=打开(文件名为“rb”)
IOError:[Errno 2]没有这样的文件或目录:“C:/Users/PC/Desktop/myfile”

我做错了什么?我应该怎么做?

语法错误是第一行末尾不应该有

“无此类文件或目录”错误是因为桌面不是位于“C:/desktop”的目录。实际上有不止一个目录的内容显示在桌面上,但您可能需要的是“C:/Users/USERNAME/desktop/”,其中USERNAME当然是您在计算机上的用户名


如果您希望访问主目录(即,不仅仅是您的主目录,还需要运行脚本的人的主目录),则可以访问HOMEDRIVE和HOMEPATH环境变量。

它是连接行末尾的冒号。它不应该在那里。

正如《疯狂刺猬》和史蒂夫·杰索普所建议的那样,我不得不转换成下面这句话

filename=os.path.join("C:/","Desktop/myfile"):
进入


因此,我只需删除冒号,写入正确的目录并写入myfile.xls,而不仅仅是myfile。

现在,我的join()行看起来是这样的:filename=os.path.join(“C:/”,“/Users/PC/Desktop/myfile”):但仍然会出现语法错误。您根本不需要加入。。只需将其设置为filename=“C:/Users/USERNAME/Desktop/myfile.xls”#或whatever@user46147:哦,对不起,我以为你得到的错误是标题“没有这样的目录”。语法错误是行的末尾不应该有冒号,我将把它添加到我的答案中。一开始我没注意到。这不是我说的吗?@Dementedhehog:是的,在我把关于冒号的编辑发布到我的答案之前7秒,你发布了关于冒号的答案。我希望我能打字足够快,让你抄袭。张贴语法错误文本。你是对的。我删除了冒号,现在没有提到语法错误。现在我得到另一个语法错误。(更新我的问题)这个问题告诉你问题是什么。您的文件名不正确。没有这样的档案。(是否缺少.xls扩展名?)
filename=os.path.join("C:/","/Users/PC/Desktop/myfile.xls")