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\parsers.py", line 1294, in __init__
self.book = xlrd.open_workbook(path_or_buf)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 400, in open_workbook
f = open(filename, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: 'C:\\RWFC\test.xls'
有帮助吗?

解决方案

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"
'\t'
>>> len("\t")
1

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)
1

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"
'C:\\RWFC\test.xls'
>>> r"C:\RWFC\test.xls"
'C:\\RWFC\\test.xls'

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

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

Either way should work.

其他提示

I was having a similar problem. I resolved the issue this way:

path = r"Drive:\path\to\your\file.extension"
workbook = xlrd.open_workbook(path) ##assuming you have imported xlrd already

Hope this helps. :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top