用python实现vCard3.0转vCard2.1

1、环境 

Windows 7或Windows 10+ Python3.7

2、代码

'''
 Function: 将手机联系人备份从vCard3.0文件转为vCard2.1文件(旧式手机需要)
 Comment:  在Python3.7下可运行,不支持头像转换。
 DATE:     2019-01-23
 Author:   me
'''

import pdb

def str_to_hex(str_to_chg):
    '''将string转换成hex形式的string(参考Quoted-printable编码)'''
    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')
try:
    with open(fp, 'r') 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()

注:码中请删除中文后再运行。如果将上述内容使用windows记事本以非utf-8格式保存,运行时会提醒编码错误。  

3、vCard2.1样本(示例文件名:Contact2.1.vcf)

 

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
 

4、vCard3.0样本(示例文件名:Contact3.0.vcf)

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:2019-01-23 16:13:34
END:VCARD

 

注:1、从web直接拷贝Contact3.0.vcf内容至windows记事本,每行内容可能会出现前导空白符,需要清除。

       2、最后一行要留一空行,web页面无法体现出来。 

 

源代码示例包:https://download.csdn.net/download/ltbook/10934576

你可能感兴趣的:(Python)