pythonexcelxlrdspyderpythonxy

xlrd python does not work - "syntax error"


I'm trying to get familiar with xlrd so I copied an example into my IDE (spyder). I'm using python(x,y) 2.7.6.1

This is my example

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)

As you can see, I listened to advice on SE here but it still does not work (syntax error). As it is advised here I have written stuff in os.path.join() in manner given in the accepted answer.

This is error log:

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: invalid syntax

UPDATE Now, when I removed colon from the end of the line with "join" i got another syntax error. This is it:

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'

What am I doing wrong? What should I do instead?


Solution

  • It's the colon at the end of the join line. It shouldn't be there.