Skip to content

Instantly share code, notes, and snippets.

@umrashrf
Forked from szczys/vcard-split.py
Last active September 7, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save umrashrf/4d402f0ac9477036f016 to your computer and use it in GitHub Desktop.
Save umrashrf/4d402f0ac9477036f016 to your computer and use it in GitHub Desktop.
#!/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