Tarifazo
Tarifazo

Reputation: 4343

TypeError while calling pandas.ExcelFile

I'm trying to open an excel file with some tables and keep getting this error:

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

This is my code:

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))

Also, I get the same error using:

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

I can open the file with open() normally.

I'm using python 3.4 and pandas 0.16.2 on windows 8.1

Any clues?

Upvotes: 1

Views: 637

Answers (1)

Tarifazo
Tarifazo

Reputation: 4343

Ok, the following two solutions worked:

xl = pd.ExcelFile(str(io))

And:

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

(thanks to SSC for the second one)

Upvotes: 1

Related Questions