Skip to content

Instantly share code, notes, and snippets.

@eloyz
Created March 22, 2012 09:54
Show Gist options
  • Save eloyz/2157425 to your computer and use it in GitHub Desktop.
Save eloyz/2157425 to your computer and use it in GitHub Desktop.
Commafiy: For those comma separated scenarios
# Used for lastname, firstname situations or
# when using incomplete addresses
# ', '.join() method doesn't account for blank items
# assumes string; else will break on strip method call
def commafy(*args, **kwargs):
delimiter = kwargs.get('delimiter', ', ')
return delimiter.join([i for i in args if i.strip()])
# ideal world: whole address
print commafy('11757 Katy Freeway, Suite 930', 'Houston', 'Texas', '77079')
# real world: partial address
print commafy('11757 Katy Freeway, Suite 930', ' ', '', '77079')
print ', '.join(['11757 Katy Freeway, Suite 930', ' ', '', '77079'])
>> 11757 Katy Freeway, Suite 930, Houston, Texas, 77079
>> 11757 Katy Freeway, Suite 930, 77079
>> 11757 Katy Freeway, Suite 930, , , 77079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment