Snippets

Brian Ward py3_standardize_character_vectors

You are viewing an old version of this snippet. View the current version.
Revised by Brian Ward e2f2315
def stand_strings( str_list, delim ):
    """Standardize capitalization and word delimiters for a list of strings.
    
    Args:
        str_list (list): A list of of input strings.
        delim (char): The desired output word delimiter - must be one of 
        ' ' (i.e. space), '-', '_', or '.'
        
    Returns:
        List of input strings, converted to uppercase with word delimiters
        converted to the user's chosen delimiter.
        
    Depends:
        Modules re and sys.
    
    Examples:
        >>>import re
        >>>import sys
        >>>ny = [' New York City ', 'New-York_City', 'new.york.city']
        >>>print(stand_strings(ny, "_"))
        ['NEW_YORK_CITY', 'NEW_YORK_CITY', 'NEW_YORK_CITY']

    """
    
    ## Check for appropriate word delimiter selection
    if delim not in [' ', '-', '_', '.']:
        sys.exit('''Please select one of the following for the output word delimiter:
            " " (space), "-", "_", "." ''')
    
    ## Convert to uppercase, strip leading/trailing whitespace        
    str_list = [x.upper().strip() for x in str_list]
    
    ## Swap out whitespace, dash, period, underscore for selected delimiter
    str_list = [re.sub('\s|\.|-|_', delim, x) for x in str_list]
    return str_list
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.