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. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.