目前,我正在处理一个Excel文件,并将其路径作为输入
myObj = ProcessExcelFile("C:\SampleExcel.xlsx")构造函数为
def __init__(self, filepath):
'''
Constructor
'''
if not os.path.isfile(filepath):
raise FileNotFoundError(filepath)
self.datawb = xlrd.open_workbook(filepath)但是现在从另一个接口中,我想使用同一个类,但它不给我发送文件,而是通过io流发送文件
data = req.stream.read(req.content_length)
file = io.BytesIO(data)当我传入我的文件时,这个文件变量作为
myObj = ProcessExcelFile(file)这给我带来了错误
TypeError: argument should be string, bytes or integer, not _io.BytesIO我想做一个init,这样它就可以像io流一样使用路径,或者如果不可能的话,我需要将io流中的文件作为优先级。
发布于 2016-10-13 08:25:20
您还需要修改类以允许流。通过open_workbook将流传递给file_contents。
def __init__(self, filepath, stream=False):
'''
Constructor
'''
if stream:
self.datawb = xlrd.open_workbook(file_contents=filepath.read())
else:
if not os.path.isfile(filepath):
raise FileNotFoundError(filepath)
self.datawb = xlrd.open_workbook(filepath)https://stackoverflow.com/questions/40015789
复制相似问题