exception handling and cosmetic changes

This commit is contained in:
iuvbio 2019-06-08 14:00:46 +02:00
parent d744ad526c
commit 9b4a272fd4
1 changed files with 11 additions and 11 deletions

View File

@ -5,16 +5,16 @@ import pandas as pd
from .tools import sanitize_df 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 # convert the sheet to a pandas.DataFrame
if isinstance(sheet, int): if not isinstance(sheet_id, (int, str)):
sheet = doc.sheets[sheet - 1] raise ValueError("Sheet id has to be either `str` or `int`")
elif isinstance(sheet, str): if isinstance(sheet_id, str):
sheets = [sheet.name for sheet in doc.sheets] sheets = [sheet.name for sheet in doc.sheets]
if sheet not in sheets: if sheet_id not in sheets:
raise ValueError("There is no sheet named {}".format(sheet)) raise ValueError("There is no sheet named {}".format(sheet_id))
sheet_idx = sheets.index(sheet) sheet_id = sheets.index(sheet_id) + 1
sheet = doc.sheets[sheet_idx] sheet = doc.sheets[sheet_id - 1]
df_dict = {} df_dict = {}
col_index = {} col_index = {}
for i, row in enumerate(sheet.rows()): for i, row in enumerate(sheet.rows()):
@ -28,7 +28,7 @@ def load_ods(doc, sheet, headers=True, columns=None):
continue continue
elif i == 0: elif i == 0:
columns = columns if columns else ( 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 # columns as lists in a dictionary
df_dict = {column: [] for column in columns} df_dict = {column: [] for column in columns}
# create index for the column headers # 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): def read_ods(file_or_path, sheet, headers=True, columns=None):
""" """
This function reads in the provided ods file and converts it to a This function reads in the provided ods file and converts it to a
dictionary. The dictionary is converted to a DataFrame. Empty rows and dictionary. The dictionary is converted to a DataFrame. Trailing empty rows
columns are dropped from the DataFrame, before it is returned. and columns are dropped from the DataFrame, before it is returned.
:param file_or_path: str :param file_or_path: str
the path to the ODS file the path to the ODS file