function parse_email_address
Parses email address strings by handling multiple addresses separated by semicolons and converting them to comma-separated format.
/tf/active/vicechatdev/msg_to_eml.py
32 - 41
simple
Purpose
This function normalizes email address strings for consistent formatting. It handles cases where multiple email addresses are separated by semicolons (common in some email clients like Outlook) and converts them to a comma-separated format. Empty or None inputs are safely handled by returning an empty string.
Source Code
def parse_email_address(address_string):
"""Parse email address strings into proper format"""
if not address_string:
return ''
# Handle multiple addresses separated by semicolons
if ';' in address_string:
addresses = address_string.split(';')
return ', '.join(addr.strip() for addr in addresses if addr.strip())
return address_string
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
address_string |
- | - | positional_or_keyword |
Parameter Details
address_string: A string containing one or more email addresses. Can be a single email address, multiple addresses separated by semicolons, or None/empty string. Expected format examples: 'user@example.com', 'user1@example.com; user2@example.com', or None.
Return Value
Returns a string containing the formatted email address(es). If input contains semicolon-separated addresses, returns them as comma-separated with whitespace trimmed. Returns empty string if input is None, empty, or contains only whitespace. Return type is always str.
Usage Example
# Example 1: Single email address
result = parse_email_address('user@example.com')
print(result) # Output: 'user@example.com'
# Example 2: Multiple semicolon-separated addresses
result = parse_email_address('user1@example.com; user2@example.com; user3@example.com')
print(result) # Output: 'user1@example.com, user2@example.com, user3@example.com'
# Example 3: Empty or None input
result = parse_email_address(None)
print(result) # Output: ''
# Example 4: Addresses with extra whitespace
result = parse_email_address('user1@example.com ; user2@example.com ')
print(result) # Output: 'user1@example.com, user2@example.com'
Best Practices
- This function does not validate email address format - it only reformats the separator characters
- The function assumes semicolons are the only alternative separator to commas
- Empty addresses in the semicolon-separated list are filtered out automatically
- The function is safe to use with None or empty string inputs
- Consider adding email validation logic if you need to ensure addresses are properly formatted
- This function does not handle email addresses with display names (e.g., 'John Doe <john@example.com>') - it treats them as plain strings
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function msg_to_eml 43.7% similar
-
function convert_european_decimals 43.4% similar
-
function smart_read_csv 43.2% similar
-
function msg_to_eml_alternative 42.7% similar
-
function main_v67 42.5% similar