Skip to content

Instantly share code, notes, and snippets.

@mattl
Forked from szczys/vcard-split.py
Last active February 2, 2021 19:35
Show Gist options
  • Save mattl/7baa0098520dfe3e3fa60aa457afdbcd to your computer and use it in GitHub Desktop.
Save mattl/7baa0098520dfe3e3fa60aa457afdbcd to your computer and use it in GitHub Desktop.
Python script for splitting a VCARD file to a user-set number of VCARDs per output file
#split vcf files
working_dir = '/home/gooserid/'
input_file = 'vCard_2.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):
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