Smarter last name first function

def last_first(name):
  Return last_name, first_name.
  Checks for already reversed names.
  Looks for some suffixes after the last name.
  Only works with one part last names.
  Leaves names like 'Charles III' as is.
  SUFFIXES = ('jr', 'jnr', 'sr', 'snr', 'ii', 'iii', 'dr', 'ddr', 'ddrs', 'pc', 'esq')
  retval = name
  if name and ',' not in name:
    parts = name.split()
    if len(parts) > 1:
      if len(parts) > 2:
        if parts[-1].lower() in SUFFIXES:
          retval = ' '.join(parts[-2:]) + ', ' + ' '.join(parts[:-1])
          retval = parts[-1] + ', ' + ' '.join(parts[:-1])
      elif parts[-1].lower() not in SUFFIXES:
        retval = parts[-1] + ', ' + ' '.join(parts[:-1])
  return retval


About freegnu

freegnu and other stuff too
This entry was posted in python and tagged , , , . Bookmark the permalink.

One Response to Smarter last name first function

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s