2017-01-19 57 views
1

我試圖打開一個Excel有一些表格文件,並不斷收到此錯誤:類型錯誤同時呼籲pandas.ExcelFile

TypeError: unsupported operand type(s) for <<: 'str' and 'int' 

這是我的代碼:

import pandas as pd 
from pathlib import Path 
import sys 

def file_lister(path, extension=None): 
    if extension is None: 
     return list(path.glob('*')) 
    else: 
     return list(path.glob('*' + extension)) 


fpath = Path().resolve().parent 
fname = 'Accounts 2017 varios.xlsx' 

try: 
    io = list(fpath.glob(fname))[0] 
except IndexError: 
    file_list = file_lister(fpath, 'xlsx') 
    raise Warning(('{} not founded. Available files:' + '\n\t{}' * len(
        file_list)).format(fname, *[file for file in file_list])) 

encoding = 'latin-1' 

xl = pd.ExcelFile(io.open(encoding=encoding)) 

而且,我得到使用相同的錯誤:

pd.read_excel(io.open(encoding=sys.getfilesystemencoding())) 

我可以用open()正常打開文件。

我使用的是Windows 8.1

任何線索蟒蛇3.4和熊貓0.16.2?

+1

在哪一行中引發異常? – EndermanAPM

+0

你可以嘗試當你使用open()時,你添加'b'來指定excel文件是一個二進制文件嗎?例如,'io.open(encoding = encoding,'rb')' – SSC

+0

io.open('rb')工作,謝謝 – Mstaino

回答

1

好了,下面的兩個解決方案的工作:

xl = pd.ExcelFile(str(io)) 

和:

xl = pd.ExcelFile(io.open('rb')) 

(感謝SSC的第二個)

相關問題