Created
March 22, 2012 09:54
-
-
Save eloyz/2157425 to your computer and use it in GitHub Desktop.
Commafiy: For those comma separated scenarios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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