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.
        str_list (list): A list of of input strings.
        delim (char): The desired output word delimiter - must be one of 
        ' ' (i.e. space), '-', '_', or '.'
        List of input strings, converted to uppercase with word delimiters
        converted to the user's chosen delimiter.
        Modules re and sys.
        >>>import re
        >>>import sys
        >>>ny = [' New York City ', 'New-York_City', '']
        >>>print(stand_strings(ny, "_"))

    ## 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

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