2017-01-12 48 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」)'? –