Skip to content

Instantly share code, notes, and snippets.

@honggoff
Last active November 9, 2016 12:27
Show Gist options
  • Save honggoff/ec4abd917061da40a94495b8cddb65aa to your computer and use it in GitHub Desktop.
Save honggoff/ec4abd917061da40a94495b8cddb65aa to your computer and use it in GitHub Desktop.
Fix for reversed first and last names in an address book.
#! /usr/bin/python
import vobject
"""
Fix for reversed first and last names in an address book.
This script go through all contacts in a vCard file, and for
each entry ask you if the first and last names should be
reversed.
"""
def fix_names(infile, ofile):
for o in vobject.readComponents(infile):
print o.getChildValue(u'n', default="ERROR")
choice = raw_input("switch? y/[n]: ")
if choice == 'y':
for child in o.getChildren():
if child.name == 'N':
n = child
n.value.given, n.value.family = n.value.family, n.value.given
if child.name == 'FN':
name_parts = child.value.split(' ')
if len(name_parts) == 2:
child.value = ' '.join(reversed(name_parts))
else:
print "not fixing full name!"
ofile.write(o.serialize())
def main():
import argparse
description = """
This script go through all contacts in a vCard file, and for
each entry ask you if the first and last names should be
reversed.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('input_file', type=file)
parser.add_argument('output_file', type=argparse.FileType('w'))
args = parser.parse_args()
fix_names(args.input_file, args.output_file)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment