Skip to content

Instantly share code, notes, and snippets.

@franklinmoy3
Created July 15, 2023 22:29
Show Gist options
  • Save franklinmoy3/d12833b7324798309303e31b4f3c1b2f to your computer and use it in GitHub Desktop.
Save franklinmoy3/d12833b7324798309303e31b4f3c1b2f to your computer and use it in GitHub Desktop.
Split Google Contacts VCF file containing multiple contact cards
import os
END_VCARD = "END:VCARD\n"
in_file = "./contacts.vcf"
out_file_format = "./contacts_out/contact_{}.vcf"
with open(in_file, "r") as input_file:
contents_as_string = input_file.read()
arr = [e+END_VCARD for e in contents_as_string.split(END_VCARD) if e]
os.makedirs(os.path.dirname(out_file_format), exist_ok=True)
for i, vcf in enumerate(arr):
out_file_name = out_file_format.format(i)
print(f"Wrote below to {out_file_name}:\n", vcf, sep="")
with open(out_file_name, "x") as output_file:
output_file.write(vcf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment