Skip to content

Instantly share code, notes, and snippets.

@johnjohndoe
Forked from umrashrf/vcard-split.py
Last active June 11, 2024 08:34
Show Gist options
  • Save johnjohndoe/dada51bb886d419334de6fe13ce6d7bb to your computer and use it in GitHub Desktop.
Save johnjohndoe/dada51bb886d419334de6fe13ce6d7bb to your computer and use it in GitHub Desktop.
Python script to split Google contacts into individual VCF files.
#!/usr/bin/python
#split vcf files
import re
working_dir = '/home/umair/Documents/Contacts/'
input_file = 'contacts starred 26-06-2014.vcf'
output_seed = 'contacts-part-'
vcards_per_file = 1
with open(working_dir + input_file,'r') as f:
count = 0
output_count = 1
results = []
for line in f:
if ("BEGIN:VCARD" in line):
count += 1
if (count <= vcards_per_file):
if line.startswith('N:'):
# LAST;FIRST -> FIRST;LAST Names
line = re.sub(r'N:([^;]*);([^;]+)(;+)', r'N:\2;\1\3', line)
results.append(line)
else:
#output file with stored values
with open(working_dir + output_seed + str(output_count) + '.vcf','w') as oFile:
for item in results:
oFile.write(item)
#increment outputfile count
output_count += 1
#clear results list and append last read line
del results[:]
results.append(line)
#set counter back to 1
count = 1
#write the last set of results to a file
with open(working_dir + output_seed + str(output_count) + '.vcf','w') as oFile:
for item in results:
oFile.write(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment