vCard是电子名片的文件格式标准。一般手机云备份通讯录导出格式支持vCard,但不同手机厂商支持的版本不一样,但可以通过Python进行转换,本文主要介绍通过Python实现vCard 2.1和3.0 互相转换的方法,以及相关的示例代码。

1、vCard格式

1)vCard2.1

BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=BC=A0;=E4=B8=89;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=BC=A0=E4=B8=89
TEL;CELL:1-300-756-0001
END:VCARD
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E6=9D=8E;=E5=9B=9B;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E6=9D=8E=E5=9B=9B
TEL;CELL:1-350-756-0001
END:VCARD

2)vCard3.0

BEGIN:VCARD
N:张;;三;;
FN:张三
TEL;CELL:139-020-12345
VERSION:3.0
UID:{12065907AB6A4B33BA359EF2A85458DF}
REV:2019-01-23 16:13:34
END:VCARD
BEGIN:VCARD
N:;;李四;;
FN:李四
TEL;HOME:123-4567
VERSION:3.0
UID:{3147AEDE309741CBA5A7906193358E14}
REV:2019-01-23 16:13:34
END:VCARD
BEGIN:VCARD
N:姓氏;名字;中间名;名称前缀;名称后缀
FN:张三
TEL;CELL:139-020-12345
VERSION:3.0
UID:{12065907AB6A4B34BA359EF2A85459DF}
REV:2021-02-21 11:24:04
END:VCARD

2、vCard 2.1转换成3.0

Python需要Python 3.7以上版本,代码如下,

import pdb
 
def str_to_hex(str_to_chg):
    tmp_bytes = bytes(str_to_chg, encoding = 'utf-8')
    tmp_chars = []
    for each_byte in  tmp_bytes:
        tmp_chars.append( '=' + str(hex(int(each_byte))).replace('0x','').upper())
    return ''.join(tmp_chars)
 
fp = r'D:\Contact3.0.vcf'
wfile = r'D:\Contact2.1.vcf'
wf = open(wfile,'w',encoding='utf-8')
try:
    with open(fp, 'r',encoding='utf-8') as file:
        all_content = file.readlines()
        ignore_lines = 0
        #pdb.set_trace()
        for line in all_content:
            if line[0:2]=='N:':
                names = line[2:].replace('\n','').split(";")
                first_name = str_to_hex(names[2])    #  3.0's family-name at pos:2
                middle_name = str_to_hex(names[0])   #  3.0's middle-name at pos:0
                last_name = str_to_hex(names[1])     #  3.0's name at pos:1
                prefix_name = str_to_hex(names[3])   #  3.0's to prefix of name at pos:3
                subfix_name = str_to_hex(names[4])   #  3.0's subfix of name at pos:4
                s = wf.write('N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:')
                s = wf.write(first_name + ';')
                s = wf.write(middle_name + ';')
                s = wf.write(last_name + ';')
                s = wf.write(prefix_name + ';')
                s = wf.write(subfix_name + '\n')
            elif line[0:3]=='FN:':
                names = line[3:]
                s = wf.write('FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:' + str_to_hex(names.replace('\n','')) + '\n')
            elif line.startswith('VERSION:'):
                s = wf.write('VERSION:2.1\n')
            elif line.startswith('UID:'):
                ignore_lines = ignore_lines + 1
                #pass
            else:
                s = wf.write(line)
                ignore_lines = ignore_lines + 1
                #pass
except Exception as ext:
    print("err:", ext)
 
wf.close()

注意:转换完成格式如导入不成功,则需要与之前导出的文件格式进行对比,可能需要调整一下。

3、vCard3.0转换成2.1

文档地址https://github.com/jowave/vcard2to3

转换方法:

git clone https://github.com/jowave/vcard2to3.git
cd vcard2to3
./vcard2to3.py your_file.vcf