jonas jonas - 3 years ago 331
Python Question

import excel file error python panda

I have trouble loading excel files into a dataframe using ExcelFile(). I have imported pandas,xlrd and openpyxl. I am using spyder for interactive data analysis.
I'm new to pandas and python, so I would appriciate an answer that is understandable for a beginner. Could someone help me?

>>> import xlrd
>>> import openpyxl
>>> from pandas import *
>>> xls = ExcelFile('C:\RWFC\test.xls')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pandas\io\", line 1294, in __init__ = xlrd.open_workbook(path_or_buf)
File "C:\Python27\lib\site-packages\xlrd\", line 400, in open_workbook
f = open(filename, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\RWFC\test.xls'

Answer Source

The problem is in this line:

>>> xls = ExcelFile('C:\RWFC\test.xls')

The backward slash has a special meaning. For example, the character "\t" in a normal string is the tab character:

>>> "\t"
>>> len("\t")

That's why in your error message:

IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\RWFC\test.xls'

You see a double slash in front of the R -- \R doesn't have any special meaning, and so it knew you meant one "real" slash:

>>> s = "\\"
>>> s
>>> print s
>>> len(s)

but \t does have a special meaning. To solve this problem you can either use a "raw string", and add "r" before the string literal:

>>> "C:\RWFC\test.xls"
>>> r"C:\RWFC\test.xls"

or, you can simply use forward slashes instead -- which Windows supports -- and avoid all the trouble:

>>> "C:/RWFC/test.xls"

Either way should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download