2017-01-12 221 views
0

我目前正试图读取使用Python 2.7.10 :: Anaconda 2.3.0(64位)在Windows上的Excel文件,并从其内容创建数据框。下面是我的代码片段:BadZipfile大熊猫XLRD错误,当打开Excel文件

import argparse 
import pandas as pd 

# xl.py 

# Adding an input argument 
parser = argparse.ArgumentParser() 
parser.add_argument("-i","--input",help="Input Excel file to generate df", 
        type=argparse.FileType('r')) 
args = parser.parse_args() 

# Reading the Excel File 
xls = pd.ExcelFile(args.input) 
df = xls.parse('Sheet 1') 

# printing for debug 
print df.head() 

在运行时执行以下操作: python xl.py -i test.xlsx

我收到一个回溯错误:

Traceback (most recent call last): 
    File ".\xl.py", line 11 
    File "C:\Users\me\Anaconda\lib\site-packages\pandas\io\excel.py", line 194, in __init__ 
     self.book = xlrd.open_workbook(file_contents=data) 
    File "C:\Users\me\Anaconda\lib\site-packages\xlrd\__init__.py", line 399, in open_workbook 
     zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents)) 
    File "C:\Users\me\Anaconda\lib\zipfile.py", line 770, in __init__ 
     self._RealGetContents() 
    File "C:\Users\me\Anaconda\zipfile.py", line 711 in _RealGetContents 
     raise BadZipfile, "File is not a zip file" 
zipfile.BadZipfile, "File is not a zip file" 

当我将我的XLSX文件转移到64位运行python 2.7.5的GNU/Linux服务器。我能够运行这个脚本没有问题。然而,我需要它在Windows中的功能,因为我将在未来使用pyinstaller使其可执行(即移除位置参数并允许人们双击它)。

任何想法为什么Windows有这个问题?

谢谢。

回答

0

编辑:已解决。我需要阅读以二进制方式arguemnt让过去这个问题上的窗口:

parser.add_argument("-i","--input",help="Input Excel file to generate df", type=argparse.FileType('r')) args = parser.parse_args()

+0

难道你的意思是'文件类型(“RB”)'? –