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?
在哪一行中引發異常? – EndermanAPM
你可以嘗試當你使用open()時,你添加'b'來指定excel文件是一個二進制文件嗎?例如,'io.open(encoding = encoding,'rb')' – SSC
io.open('rb')工作,謝謝 – Mstaino