From 9b4a272fd48cd59aa4607a40c430febede3fe88c Mon Sep 17 00:00:00 2001 From: iuvbio Date: Sat, 8 Jun 2019 14:00:46 +0200 Subject: [PATCH] exception handling and cosmetic changes --- pandas_ods_reader/parser.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas_ods_reader/parser.py b/pandas_ods_reader/parser.py index 1334eae..8b0ffbb 100644 --- a/pandas_ods_reader/parser.py +++ b/pandas_ods_reader/parser.py @@ -5,16 +5,16 @@ import pandas as pd from .tools import sanitize_df -def load_ods(doc, sheet, headers=True, columns=None): +def load_ods(doc, sheet_id, headers=True, columns=None): # convert the sheet to a pandas.DataFrame - if isinstance(sheet, int): - sheet = doc.sheets[sheet - 1] - elif isinstance(sheet, str): + if not isinstance(sheet_id, (int, str)): + raise ValueError("Sheet id has to be either `str` or `int`") + if isinstance(sheet_id, str): sheets = [sheet.name for sheet in doc.sheets] - if sheet not in sheets: - raise ValueError("There is no sheet named {}".format(sheet)) - sheet_idx = sheets.index(sheet) - sheet = doc.sheets[sheet_idx] + if sheet_id not in sheets: + raise ValueError("There is no sheet named {}".format(sheet_id)) + sheet_id = sheets.index(sheet_id) + 1 + sheet = doc.sheets[sheet_id - 1] df_dict = {} col_index = {} for i, row in enumerate(sheet.rows()): @@ -28,7 +28,7 @@ def load_ods(doc, sheet, headers=True, columns=None): continue elif i == 0: columns = columns if columns else ( - ["column_%s" % j for j in range(len(row))]) + [f"column_{j}" for j in range(len(row))]) # columns as lists in a dictionary df_dict = {column: [] for column in columns} # create index for the column headers @@ -49,8 +49,8 @@ def load_ods(doc, sheet, headers=True, columns=None): def read_ods(file_or_path, sheet, headers=True, columns=None): """ This function reads in the provided ods file and converts it to a - dictionary. The dictionary is converted to a DataFrame. Empty rows and - columns are dropped from the DataFrame, before it is returned. + dictionary. The dictionary is converted to a DataFrame. Trailing empty rows + and columns are dropped from the DataFrame, before it is returned. :param file_or_path: str the path to the ODS file