def get_data(afile, file_type=None, streaming=False, **keywords): """Get data from an excel file source :param filename: actual file name, a file stream or actual content :param sheet_name: the name of the sheet to be loaded :param sheet_index: the index of the sheet to be loaded :param file_type: used only when filename is not a physial file name :param keywords: any other parameters :returns: an array if it is a single sheet, an ordered dictionary otherwise """ if isstream(afile) and file_type is None: file_type = constants.FILE_FORMAT_CSV if isstream(afile): data = load_data(file_stream=afile, file_type=file_type, **keywords) else: if afile is not None and file_type is not None: data = load_data(file_content=afile, file_type=file_type, **keywords) else: data = load_data(file_name=afile, file_type=file_type, **keywords) if streaming is False: for key in data.keys(): data[key] = list(data[key]) return data
def get_data(afile, file_type=None, streaming=False, **keywords): """Get data from an excel file source :param afile: a file name, a file stream or actual content :param sheet_name: the name of the sheet to be loaded :param sheet_index: the index of the sheet to be loaded :param file_type: used only when filename is not a physial file name :param streaming: toggles the type of returned data. The values of the returned dictionary remain as generator if it is set to True. Default is False. :param library: explicitly name a library for use. e.g. library='pyexcel-ods' :param auto_detect_float: defaults to True :param auto_detect_int: defaults to True :param auto_detect_datetime: defaults to True :param ignore_infinity: defaults to True :param keywords: any other library specific parameters :returns: an ordered dictionary """ if isstream(afile) and file_type is None: file_type = constants.FILE_FORMAT_CSV if isstream(afile): data = load_data(file_stream=afile, file_type=file_type, **keywords) else: if afile is not None and file_type is not None: data = load_data(file_content=afile, file_type=file_type, **keywords) else: data = load_data(file_name=afile, file_type=file_type, **keywords) if streaming is False: for key in data.keys(): data[key] = list(data[key]) return data
def save_data(afile, data, file_type=None, **keywords): """Save data to an excel file source Your data can be an array or an ordered dictionary :param filename: actual file name, a file stream or actual content :param data: the data to be saved :param file_type: used only when filename is not a physial file name :param keywords: any other parameters that python csv module's `fmtparams <https://docs.python.org/release/3.1.5/ library/csv.html#dialects-and-formatting-parameters>`_ """ to_store = data if isinstance(data, list) or is_generator(data): single_sheet_in_book = True to_store = {constants.DEFAULT_SHEET_NAME: data} else: if PY2: keys = data.keys() else: keys = list(data.keys()) if len(keys) == 1: single_sheet_in_book = True else: single_sheet_in_book = False if isstream(afile) and file_type is None: file_type = constants.FILE_FORMAT_CSV store_data(afile, to_store, file_type=file_type, single_sheet_in_book=single_sheet_in_book, **keywords)
def open_stream(self, file_stream, **keywords): """ open a file with unlimited keywords for reading keywords are passed on to individual readers """ if isstream(file_stream): if PY2: if hasattr(file_stream, "seek"): file_stream.seek(0) else: # python 2 # Hei zipfile in odfpy would do a seek # but stream from urlib cannot do seek file_stream = _convert_content_to_stream( file_stream.read(), self._file_type ) else: from io import UnsupportedOperation try: file_stream.seek(0) except UnsupportedOperation: # python 3 file_stream = _convert_content_to_stream( file_stream.read(), self._file_type ) self._file_stream = file_stream self._keywords = keywords else: raise IOError(MESSAGE_WRONG_IO_INSTANCE)
def save_data(afile, data, file_type=None, **keywords): """Save data to an excel file source Your data must be a dictionary :param filename: actual file name, a file stream or actual content :param data: a dictionary but an ordered dictionary is preferred :param file_type: used only when filename is not a physial file name :param library: explicitly name a library for use. e.g. library='pyexcel-ods' :param keywords: any other parameters that python csv module's `fmtparams <https://docs.python.org/release/3.1.5/library/csv.html#dialects-and-formatting-parameters>`_ """ # noqa to_store = data is_list = isinstance(data, (list, GeneratorType)) if is_list: single_sheet_in_book = True to_store = {constants.DEFAULT_SHEET_NAME: data} else: if PY2: keys = data.keys() else: keys = list(data.keys()) single_sheet_in_book = len(keys) == 1 no_file_type = isstream(afile) and file_type is None if no_file_type: file_type = constants.FILE_FORMAT_CSV store_data(afile, to_store, file_type=file_type, single_sheet_in_book=single_sheet_in_book, **keywords)
def open_stream(self, file_stream, **keywords): """ open a file with unlimited keywords for reading keywords are passed on to individual readers """ if isstream(file_stream): if PY2: if hasattr(file_stream, "seek"): file_stream.seek(0) else: # python 2 # Hei zipfile in odfpy would do a seek # but stream from urlib cannot do seek file_stream = _convert_content_to_stream( file_stream.read(), self._file_type) else: from io import UnsupportedOperation try: file_stream.seek(0) except UnsupportedOperation: # python 3 file_stream = _convert_content_to_stream( file_stream.read(), self._file_type) self._file_stream = file_stream self._keywords = keywords else: raise IOError(MESSAGE_WRONG_IO_INSTANCE)
def open_stream(self, file_stream, **keywords): """ open a file stream with unlimited keywords for writing keywords are passed on to individual writers """ if not isstream(file_stream): raise IOError(MESSAGE_ERROR_03) self.open(file_stream, **keywords)
def _get_data(afile, file_type=None, **keywords): if isstream(afile): keywords.update( dict(file_stream=afile, file_type=file_type or constants.FILE_FORMAT_CSV)) else: if afile is None or file_type is None: keywords.update(dict(file_name=afile, file_type=file_type)) else: keywords.update(dict(file_content=afile, file_type=file_type)) return load_data(**keywords)
def open_stream(self, file_stream, **keywords): """ open a file with unlimited keywords for reading keywords are passed on to individual readers """ if isstream(file_stream): self._file_stream = file_stream self._keywords = keywords else: raise IOError(MESSAGE_WRONG_IO_INSTANCE)
def store_data(afile, data, file_type=None, **keywords): """Non public function to store data to afile :param filename: actual file name, a file stream or actual content :param data: the data to be written :param file_type: used only when filename is not a physial file name :param keywords: any other parameters """ if isstream(afile): keywords.update(dict(file_stream=afile, file_type=file_type)) else: keywords.update(dict(file_name=afile, file_type=file_type)) with get_writer(**keywords) as writer: writer.write(data)
def _get_data(afile, file_type=None, **keywords): if isstream(afile): keywords.update( dict( file_stream=afile, file_type=file_type or constants.FILE_FORMAT_CSV, ) ) else: if afile is None or file_type is None: keywords.update(dict(file_name=afile, file_type=file_type)) else: keywords.update(dict(file_content=afile, file_type=file_type)) return load_data(**keywords)
def open_stream(self, file_stream, **keywords): """ open a file with unlimited keywords for reading keywords are passed on to individual readers """ if isstream(file_stream): from io import UnsupportedOperation try: file_stream.seek(0) except UnsupportedOperation: # python 3 file_stream = _convert_content_to_stream( file_stream.read(), self._file_type ) self._file_stream = file_stream self._keywords = keywords else: raise IOError(MESSAGE_WRONG_IO_INSTANCE)