add tests
This commit is contained in:
parent
4e282676da
commit
0e89be912e
|
|
@ -12,12 +12,12 @@ def ods_info(doc):
|
|||
sheet.nrows(), sheet.ncols()))
|
||||
|
||||
|
||||
def load_ods(doc, sheet, header=True, columns=None):
|
||||
def load_ods(doc, sheet, headers=True, columns=None):
|
||||
# convert the sheet to a pandas.DataFrame
|
||||
if isinstance(sheet, int):
|
||||
sheet = doc.sheets[sheet - 1]
|
||||
elif isinstance(sheet, str):
|
||||
sheets = [sheet for sheet in doc.sheets]
|
||||
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)
|
||||
|
|
@ -26,14 +26,14 @@ def load_ods(doc, sheet, header=True, columns=None):
|
|||
col_index = {}
|
||||
for i, row in enumerate(sheet.rows()):
|
||||
# row is a list of cells
|
||||
if header and i == 0:
|
||||
if headers and i == 0:
|
||||
# columns as lists in a dictionary
|
||||
df_dict = {cell.value: [] for cell in row if cell.value}
|
||||
# create index for the column headers
|
||||
col_index = {
|
||||
j: cell.value for j, cell in enumerate(row) if cell.value}
|
||||
continue
|
||||
elif not header and i == 0:
|
||||
elif not headers and i == 0:
|
||||
columns = columns if columns else (
|
||||
["Column_%s" % j for j in range(len(row))])
|
||||
# columns as lists in a dictionary
|
||||
|
|
@ -71,7 +71,7 @@ def sanitize_df(df):
|
|||
return df
|
||||
|
||||
|
||||
def read_ods(file_or_path, sheet, header=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
|
||||
|
|
@ -90,5 +90,5 @@ def read_ods(file_or_path, sheet, header=True, columns=None):
|
|||
the ODS file as a pandas DataFrame
|
||||
"""
|
||||
doc = ezodf.opendoc(file_or_path)
|
||||
df = load_ods(doc, sheet, header, columns)
|
||||
df = load_ods(doc, sheet, headers, columns)
|
||||
return sanitize_df(df)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,43 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
from pandas_ods_reader import read_ods
|
||||
from pandas_ods_reader.read_ods import read_ods
|
||||
|
||||
|
||||
root = os.path.dirname(os.path.abspath(read_ods.__file__))
|
||||
test_dir = os.path.join(root, "tests")
|
||||
root = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
def test_header_file():
|
||||
example = "example_headers.ods"
|
||||
path = os.path.join(root, example)
|
||||
print("Test sheet by index")
|
||||
df = read_ods(path, 1)
|
||||
print(df)
|
||||
print("Test sheet by name")
|
||||
df = read_ods(path, "Sheet1")
|
||||
print(df)
|
||||
|
||||
|
||||
def test_no_header_file():
|
||||
example = "example_no_headers.ods"
|
||||
path = os.path.join(root, example)
|
||||
print("Test sheet by index and default columns")
|
||||
df = read_ods(path, 1, headers=False)
|
||||
print(df)
|
||||
print("Test sheet by name and default columns")
|
||||
df = read_ods(path, "Sheet1", headers=False)
|
||||
print(df)
|
||||
print("Test sheet by index and specific columns")
|
||||
columns = ["A", "B", "C", "D", "E"]
|
||||
df = read_ods(path, 1, headers=False, columns=columns)
|
||||
print(df)
|
||||
|
||||
|
||||
def main():
|
||||
test_header_file()
|
||||
test_no_header_file()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
status = main()
|
||||
sys.exit(status)
|
||||
|
|
|
|||
Loading…
Reference in New Issue