Hydrologic Projects
ESPAM2 Recharge Tools 1.5 Documentation
espam.tablereader
The tablereader module contains methods for reading from csv, Excel, and Excel 2007 file formats. Unless the file format is known before hand, all file read calls should go through the get_table_reader method.
Excel and Excel 2007 support is optional and will run fine without those libraries.
- class espam.tablereader.CsvTableReader(file_name)
Class which enables read-only access to a CSV file
The CSVTableReader class provides a one-way read only access to a CSV file, and has methods to query column names, and column name indices.
- get_column_names()
Get a list of column names in file
Get a list of column names in file that are in the order of appearance in the file. Assume that the first row in the file are column names
Returns: list – List of column names >>> print my_reader_object.get_column_names() ["first_col", "second_col", "third_col"]
- rows()
A generator for accessing rows in tablereader
Use rows to access rows within table. The first row will be ignored, since it is assumed that the first row contains headers. Each row will be returned as a list of row values in the same order of appearance in the table.
Yields : list – Current row in list format >>> for row in my_reader_instance.rows: >>> ... print row >>> ... break ['rowvalue1', 'rowvalue2', 'rowvalue3']
- class espam.tablereader.Excel2007TableReader(file_name, sheet=None)
Class which enables read-only access to an Excel 2007 (xlsx) file
The Excel2007TableReader class provides a one-way read only access to an Excel 2007 (xlsx) file, and has methods to query column names, and column name indices.
- get_column_names()
Get a list of column names in file
Get a list of column names in file that are in the order of appearance in the file. Assume that the first row in the file are column names
Returns: list – List of column names >>> print my_reader_object.get_column_names() ["first_col", "second_col", "third_col"]
- get_column_names_and_index_list()
Get a dictionary of column names and indexes in file
Get a dictionary of column names in file that are keyed by the index of the order of appearance in the file. Assume that the first row in the file are column names
Returns: dict – Dictionary of column names keyed on the column index >>> print my_reader_object.get_column_names_and_index_list() {0 : "first_col", 1 : "second_col", 2 : "third_col" }
- rows()
A generator for accessing rows in tablereader
Use rows to access rows within table. The first row will be ignored, since it is assumed that the first row contains headers. Each row will be returned as a list of row values in the same order of appearance in the table.
Yields : list – Current row in list format >>> for row in my_reader_instance.rows: >>> ... print row >>> ... break ['rowvalue1', 'rowvalue2', 'rowvalue3']
- set_sheet(sheet_name)
Define a worksheet to read from
- class espam.tablereader.ExcelTableReader(file_name, sheet=None)
Class which enables read-only access to pre Excel 2007 (xls) files
The ExcelTableReader class provides a one-way read only access to files created in Excel prior to Excel 2007 (xls) file, and has methods to query column names, and column name indices.
- get_column_names()
Get a list of column names in file
Get a list of column names in file that are in the order of appearance in the file. Assume that the first row in the file are column names
Returns: list – List of column names >>> print my_reader_object.get_column_names() ["first_col", "second_col", "third_col"]
- get_column_names_and_index_list()
Get a dictionary of column names and indexes in file
Get a dictionary of column names in file that are keyed by the index of the order of appearance in the file. Assume that the first row in the file are column names
Returns: dict – Dictionary of column names keyed on the column index >>> print my_reader_object.get_column_names_and_index_list() {0 : "first_col", 1 : "second_col", 2 : "third_col" }
- rows()
A generator for accessing rows in tablereader
Use rows to access rows within table. The first row will be ignored, since it is assumed that the first row contains headers. Each row will be returned as a list of row values in the same order of appearance in the table.
Yields : list – Current row in list format >>> for row in my_reader_instance.rows: >>> ... print row >>> ... break ['rowvalue1', 'rowvalue2', 'rowvalue3']
- set_sheet(sheet_name)
Define a worksheet to read from
- espam.tablereader.get_table_reader(file_name)
Method to retrieve file TableReader object
Determine which type of file TableReader to open based on the extension of the file. The only valid extensions are xls, xlsx, and csv. This is the preferred way to fetch a reader object.
Parameters: file_name (str.) – Path to file >>> my_reader_csv_instance = get_table_reader('mycsvfile.csv') >>> print isinstance(my_reader_csv_instance,espam.tablereader.CsvTableReader) True >>> my_reader_xls_instance = get_table_reader('myxlsfile.xls') >>> print isinstance(my_reader_csv_instance,espam.tablereader.ExcelTableReader) True >>> my_reader_xlsx_instance = get_table_reader ('myxlsxfile.xlsx') >>> print isinstance(my_reader_csv_instance,espam.tablereader.Excel2007TableReader) True