### Apply Stream Processors for Data Transformation Source: https://context7.com/okfn/messytables/llms.txt Demonstrates applying built-in stream processors like headers, offset, null, and type processors to transform CSV data. Includes an example of a custom processor to filter empty rows. ```python from messytables import (CSVTableSet, headers_guess, headers_processor, offset_processor, type_guess, types_processor, null_processor) with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # 1. Detect and apply headers offset, headers = headers_guess(row_set.sample) row_set.register_processor(headers_processor(headers)) # 2. Skip to data rows (after header) row_set.register_processor(offset_processor(offset + 1)) # 3. Replace null-like values with None row_set.register_processor(null_processor(['', 'N/A', 'NULL', '-'])) # 4. Detect and apply types types = type_guess(row_set.sample, strict=True) row_set.register_processor(types_processor(types)) # Process with all transformations applied for row in row_set: print({cell.column: cell.value for cell in row}) ``` ```python # Custom processor example def filter_empty_rows(row_set, row): """Drop rows where all cells are empty""" if all(cell.empty for cell in row): return None # Drop row return row with open('sparse_data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] row_set.register_processor(filter_empty_rows) for row in row_set: print([cell.value for cell in row]) ``` -------------------------------- ### Implement a Complete Data Processing Pipeline Source: https://context7.com/okfn/messytables/llms.txt Demonstrates a full pipeline including auto-detection of file formats, header detection, null value handling, and type conversion. ```python from messytables import (any_tableset, headers_guess, headers_processor, offset_processor, type_guess, types_processor, null_processor, headers_make_unique) def process_messy_file(filepath): """Complete pipeline for processing messy tabular data.""" with open(filepath, 'rb') as fh: # Auto-detect file format ext = filepath.split('.')[-1] if '.' in filepath else '' table_set = any_tableset(fh, extension=ext) results = [] for table in table_set.tables: print(f"Processing table: {table.name}") # Step 1: Detect headers offset, headers = headers_guess(table.sample) headers = headers_make_unique(headers) table.register_processor(headers_processor(headers)) # Step 2: Skip to data table.register_processor(offset_processor(offset + 1)) # Step 3: Handle null values table.register_processor(null_processor(['', 'N/A', 'NULL', 'n/a', '-'])) # Step 4: Type conversion types = type_guess(table.sample, strict=True) table.register_processor(types_processor(types)) # Step 5: Convert to records records = list(table.dicts()) results.append({ 'name': table.name, 'headers': headers, 'types': [str(t) for t in types], 'row_count': len(records), 'data': records }) return results # Usage results = process_messy_file('messy_data.xlsx') for table_result in results: print(f"\nTable: {table_result['name']}") print(f"Columns: {table_result['headers']}") print(f"Types: {table_result['types']}") print(f"Rows: {table_result['row_count']}") for row in table_result['data'][:5]: print(row) ``` -------------------------------- ### Run tests for messytables Source: https://github.com/okfn/messytables/blob/master/CONTRIBUTING.md Commands to set up the development environment and execute the test suite. ```bash source pyenv/messytables/bin/activate python setup.py develop pip install -r requirements-test.txt nosetests ``` -------------------------------- ### Load and Process CSV Data with Messytables Source: https://github.com/okfn/messytables/blob/master/doc/index.md Demonstrates loading a CSV file, guessing headers and types, and applying processors for data cleaning and transformation. Ensure the file is opened in binary mode ('rb'). ```python from messytables import CSVTableSet, type_guess, \ types_processor, headers_guess, headers_processor, \ offset_processor, any_tableset fh = open('messy.csv', 'rb') # Load a file object: table_set = CSVTableSet(fh) # If you aren't sure what kind of file it is, you can use # any_tableset. #table_set = any_tableset(fh) # A table set is a collection of tables: row_set = table_set.tables[0] # A row set is an iterator over the table, but it can only # be run once. To peek, a sample is provided: print row_set.sample.next() # guess header names and the offset of the header: offset, headers = headers_guess(row_set.sample) row_set.register_processor(headers_processor(headers)) # add one to begin with content, not the header: row_set.register_processor(offset_processor(offset + 1)) # guess column types: types = type_guess(row_set.sample, strict=True) # and tell the row set to apply these types to # each row when traversing the iterator: row_set.register_processor(types_processor(types)) # now run some operation on the data: for row in row_set: do_something(row) ``` -------------------------------- ### Convert Row Set to JSON Table Schema Source: https://context7.com/okfn/messytables/llms.txt Demonstrates converting a row set into JSON Table Schema format using `rowset_as_jts` for automatic schema generation. Also shows manual creation using `headers_and_typed_as_jts` with custom headers and types. ```python from messytables import CSVTableSet, rowset_as_jts, headers_and_typed_as_jts from messytables import headers_guess, type_guess with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Auto-generate JSON Table Schema schema = rowset_as_jts(row_set) print(schema) ``` ```python # Manual schema creation with custom headers/types with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] offset, headers = headers_guess(row_set.sample) types = type_guess(row_set.sample) # Convert types to JTS format strings type_strings = [] for t in types: if 'Integer' in str(t): type_strings.append('integer') elif 'Decimal' in str(t) or 'Float' in str(t): type_strings.append('number') elif 'Date' in str(t): type_strings.append('date') elif 'Bool' in str(t): type_strings.append('boolean') else: type_strings.append('string') schema = headers_and_typed_as_jts(headers, type_strings) print(schema) ``` -------------------------------- ### Guess Headers with headers_guess Source: https://context7.com/okfn/messytables/llms.txt Identify the header row in messy data and apply processors to register headers and skip offset rows. ```python from messytables import CSVTableSet, headers_guess, headers_processor, offset_processor with open('messy_data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Guess header position and column names offset, headers = headers_guess(row_set.sample) print(f"Header found at row {offset}: {headers}") # Apply headers to cells row_set.register_processor(headers_processor(headers)) # Skip rows before and including header row_set.register_processor(offset_processor(offset + 1)) # Now iterate with named columns for row in row_set: for cell in row: print(f"{cell.column}: {cell.value}") ``` -------------------------------- ### Auto-detect file formats with any_tableset Source: https://context7.com/okfn/messytables/llms.txt Use any_tableset to automatically identify and parse tabular data based on file extensions, MIME types, or content inspection. ```python from messytables import any_tableset # Auto-detect file format from extension with open('data.csv', 'rb') as fh: table_set = any_tableset(fh, extension='csv') row_set = table_set.tables[0] for row in row_set: print([cell.value for cell in row]) # Auto-detect from MIME type with open('spreadsheet.xlsx', 'rb') as fh: table_set = any_tableset(fh, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') for table in table_set.tables: print(f"Table: {table.name}") # Let magic library detect format automatically with open('unknown_data', 'rb') as fh: table_set = any_tableset(fh, auto_detect=True) row_set = table_set.tables[0] for row in row_set.sample: print([cell.value for cell in row]) ``` -------------------------------- ### Working with MessyTables Cell Objects Source: https://context7.com/okfn/messytables/llms.txt Illustrates how to create and interact with `Cell` objects, which represent individual data values. Shows programmatic creation and accessing cell properties like value, column, type, and emptiness from parsed data. ```python from messytables import Cell, StringType, IntegerType, DateType from messytables import CSVTableSet, headers_processor, types_processor, type_guess # Create cells programmatically cell1 = Cell("Hello", column="greeting", type=StringType()) cell2 = Cell(42, column="count", type=IntegerType()) print(f"Cell value: {cell1.value}, column: {cell1.column}, empty: {cell1.empty}") print(f"Cell value: {cell2.value}, column: {cell2.column}, type: {cell2.type}") ``` ```python # Working with cells from parsed data with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] types = type_guess(row_set.sample) row_set.register_processor(types_processor(types)) for row in row_set: for cell in row: if not cell.empty: print(f"Column: {cell.column or 'unnamed'}") print(f"Value: {cell.value}") print(f"Type: {cell.type}") print(f"Python type: {type(cell.value)}") print("---") ``` -------------------------------- ### Guess Column Types with type_guess Source: https://context7.com/okfn/messytables/llms.txt Analyze sample data to determine column types and apply conversion processors, with support for strict or non-strict validation. ```python from messytables import CSVTableSet, type_guess, types_processor from messytables import StringType, IntegerType, DecimalType, DateType, BoolType with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Guess types from sample (non-strict allows partial matches) types = type_guess(row_set.sample, strict=False) print("Detected types:", types) # Apply types to convert cell values row_set.register_processor(types_processor(types)) for row in row_set: for cell in row: print(f"{cell.value} -> {type(cell.value).__name__} ({cell.type})") # Strict mode: type must match ALL values in column with open('clean_data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Strict mode fails if any cell doesn't match types = type_guess(row_set.sample, strict=True) row_set.register_processor(types_processor(types, strict=True)) for row in row_set: print([cell.value for cell in row]) ``` -------------------------------- ### Initialize and Configure Tablesorter Plugin Source: https://github.com/okfn/messytables/blob/master/horror/html.html This JavaScript code initializes the Tablesorter plugin for a table with the ID 'MainTable'. It specifies custom sorters for different columns, including 'digit', 'text', and 'floatWithThousandSeparator'. ```javascript footnoteDescriptions = []; $(document).ready(function() { //Activate tablesorter plugin $("#MainTable").tablesorter( { headers: { 0 : { sorter: "digit" }, 1 : { sorter: "text" }, 2 : { sorter: "floatWithThousandSeparator" }, 3 : { sorter: "floatWithThousandSeparator" }, }, }); //assign the sortStart event, to show a waiting message when ordering $("#MainTable").bind("sortStart",function() { $("#waitingDiv").show(); }).bind("sortEnd",function() { $("#waitingDiv").hide(); }); //assign the footnote description to each item $.each(footnoteDescriptions, function(footnoteNumber,footnoteDescription){ footnoteDescription = "offsetx=\[-250\] offsety=\[10\] cssheader=\[definition\] header=\[ Note\] body=\[
" + footnoteDescription + "
"]; $(".footnote.footnoteNumber" + footnoteNumber).attr("title", footnoteDescription); }); } ); ``` -------------------------------- ### Convert RowSets to Dictionaries in Python Source: https://context7.com/okfn/messytables/llms.txt Converts rows to ordered dictionaries keyed by column name. Use the sample=True parameter to read only cached sample data for large files. ```python from messytables import CSVTableSet, headers_guess, headers_processor, offset_processor with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Set up headers offset, headers = headers_guess(row_set.sample) row_set.register_processor(headers_processor(headers)) row_set.register_processor(offset_processor(offset + 1)) # Convert to dictionaries for record in row_set.dicts(): print(record) # OrderedDict([('name', 'John'), ('age', '30'), ('city', 'NYC')]) # Access by column name print(f"Name: {record['name']}") # Sample mode for preview with open('large_file.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] offset, headers = headers_guess(row_set.sample) row_set.register_processor(headers_processor(headers)) row_set.register_processor(offset_processor(offset + 1)) # Only read cached sample data for record in row_set.dicts(sample=True): print(record) ``` -------------------------------- ### Ensure Unique Headers with headers_make_unique Source: https://context7.com/okfn/messytables/llms.txt Applies the `headers_make_unique` function to ensure all column headers are unique by appending numeric suffixes. Shows usage with and without a maximum length constraint. ```python from messytables import headers_guess, headers_make_unique, CSVTableSet with open('duplicate_headers.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] offset, headers = headers_guess(row_set.sample) print(f"Original headers: {headers}") # e.g., ['Name', 'Value', 'Value', 'Value'] # Make headers unique unique_headers = headers_make_unique(headers) print(f"Unique headers: {unique_headers}") # e.g., ['Name', 'Value_1', 'Value_2', 'Value_3'] # With max length constraint (useful for databases) short_headers = headers_make_unique(headers, max_length=10) print(f"Short unique headers: {short_headers}") ``` -------------------------------- ### Read Excel files with XLSTableSet Source: https://context7.com/okfn/messytables/llms.txt XLSTableSet supports both XLS and XLSX formats, allowing access to multiple worksheets and cell-level properties like formatting. ```python from messytables import XLSTableSet # Read from file path table_set = XLSTableSet(filename='spreadsheet.xlsx') # Access all sheets for sheet in table_set.tables: print(f"Sheet: {sheet.name}") for row in sheet.sample: print([cell.value for cell in row]) # Read from file object with encoding override with open('legacy.xls', 'rb') as fh: table_set = XLSTableSet(fileobj=fh, encoding='cp1252') row_set = table_set.tables[0] for row in row_set: for cell in row: # Access cell properties (bold, font, colors) if cell.properties.get('bold', False): print(f"Bold value: {cell.value}") print(f"{cell.value} (type: {cell.type})") # Access specific sheet by name table_set = XLSTableSet(filename='workbook.xlsx') sheet = table_set['Sheet1'] for row in sheet: print([cell.value for cell in row]) ``` -------------------------------- ### Read ODS Files with ODSTableSet Source: https://context7.com/okfn/messytables/llms.txt Parse OpenDocument Spreadsheet files with support for automatic type detection and optional sample window sizing. ```python from messytables import ODSTableSet # Read ODS file with open('spreadsheet.ods', 'rb') as fh: table_set = ODSTableSet(fh) for sheet in table_set.tables: print(f"Sheet: {sheet.name}") for row in sheet: for cell in row: print(f"{cell.value} (type: {cell.type})") # Access specific sheet and handle typed values with open('data.ods', 'rb') as fh: table_set = ODSTableSet(fh, window=500) # Limit sample window row_set = table_set.tables[0] for row in row_set: values = [(cell.value, cell.type) for cell in row] print(values) ``` -------------------------------- ### Read CSV files with CSVTableSet Source: https://context7.com/okfn/messytables/llms.txt CSVTableSet provides robust CSV parsing with support for custom delimiters, encodings, and automatic dialect detection. ```python from messytables import CSVTableSet # Basic CSV reading with auto-detection with open('data.csv', 'rb') as fh: table_set = CSVTableSet(fh) row_set = table_set.tables[0] # Read sample (cached for analysis) for row in row_set.sample: print([cell.value for cell in row]) # Custom delimiter and encoding with open('data.tsv', 'rb') as fh: table_set = CSVTableSet(fh, delimiter='\t', encoding='utf-8') row_set = table_set.tables[0] for row in row_set: values = [cell.value for cell in row] print(values) # Pipe-separated values with custom quote character with open('data.psv', 'rb') as fh: table_set = CSVTableSet(fh, delimiter='|', quotechar='"', skipinitialspace=True) row_set = table_set.tables[0] for row in row_set: print([cell.value for cell in row]) ``` -------------------------------- ### Read HTML Table Data Source: https://context7.com/okfn/messytables/llms.txt Access HTML-specific cell properties like colspan and rowspan while iterating through table rows. ```python with open('report.html', 'rb') as fh: table_set = HTMLTableSet(fileobj=fh) row_set = table_set.tables[0] for row in row_set: for cell in row: # Access HTML-specific properties colspan = cell.properties.get('colspan', 1) rowspan = cell.properties.get('rowspan', 1) if colspan > 1 or rowspan > 1: print(f"Spanned cell: {cell.value} ({colspan}x{rowspan})") ``` -------------------------------- ### Read ZIP Archives with ZIPTableSet Source: https://context7.com/okfn/messytables/llms.txt Extract and parse tabular data from within ZIP archives, supporting automatic format detection for contained files. ```python from messytables import ZIPTableSet # Read tables from ZIP file with open('archive.zip', 'rb') as fh: table_set = ZIPTableSet(fh) # All tables from all supported files in the ZIP for table in table_set.tables: print(f"Table: {table.name}") for row in table.sample: print([cell.value for cell in row]) # ZIP containing multiple CSV/Excel files with open('data_bundle.zip', 'rb') as fh: table_set = ZIPTableSet(fh) print(f"Found {len(table_set.tables)} tables in archive") for table in table_set.tables: print(f"\nProcessing: {table.name}") for row in table: print([cell.value for cell in row]) ``` -------------------------------- ### Read HTML tables with HTMLTableSet Source: https://context7.com/okfn/messytables/llms.txt HTMLTableSet extracts tabular data from HTML documents, handling complex structures like nested tables and span attributes. ```python from messytables import HTMLTableSet # Read from file table_set = HTMLTableSet(filename='page.html') # Iterate through all tables found for table in table_set.tables: print(f"Table: {table.name}") for row in table: values = [cell.value for cell in row] print(values) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.