Skip to content

Instantly share code, notes, and snippets.

@yin8086
Last active November 18, 2023 04:46
Show Gist options
  • Save yin8086/4132006 to your computer and use it in GitHub Desktop.
Save yin8086/4132006 to your computer and use it in GitHub Desktop.
VCard process to get the person who has phone numbers
# -*- coding: utf-8 -*-
import vobject, sys, codecs
from os import path
def hasTel(lineList):
for line in lineList:
if line[:len('TEL;')] == 'TEL;':
return True
return False
if len(sys.argv)==1:
print u'缺少参数'
sys.exit(0)
elif not path.isfile(sys.argv[1]):
print u'目标文件不存在'
sys.exit(0)
srcVCF = codecs.open(sys.argv[1], 'rb', 'utf8')
destVCF = codecs.open(sys.argv[1][:-4]+'_new.vcf', 'wb', 'utf8')
# srcVCF = open(sys.argv[1], 'rb')
# destVCF = open(sys.argv[1][:-4]+'_new.vcf', 'wb')
state = 0
outString = []
for line in srcVCF:
if line[:len('BEGIN:VCARD')] == 'BEGIN:VCARD' and state ==0:
outString.append(line)
state = 1
elif line[:len('END:VCARD')] == 'END:VCARD' and state == 1:
if hasTel(outString):
for strEle in outString:
destVCF.write(strEle)
outString = []
destVCF.write(line)
state = 0
elif state == 1:
outString.append(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment