python转换 vcf到csv 小米通信录

手机的通信录越来越多人了,一个个手动编辑感觉太麻烦了,动了用python转换的想法,可惜一直只是一个想法。
最近有些时间,还是尝试了一下
小米手机上的通信录和云上导出的vsf文件,各种不同,后来又跑去QQ同步助手里面把之前的备份下了下来,各个版本,都不知道哪些数据有用,纠结许久,左右试了一下。发现有用的,大概就是名字,备注,和电话号码,而邮箱,头像这些可以忽略

with open("00001.vcf", 'r', encoding='utf-8') as rf, open('00001.csv', 'w') as wf:
    content = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
    tel = 4
    wf.write("名字,备注,家庭电话,工作电话,其他电话,\n")
    for line in rf.readlines():
        if line.startswith('FN:'):content[0] = line[3:].strip()
        elif line.startswith("ORG;CHARSET=UTF-8"):content[1] = line[18:].strip()
        elif line.startswith("TEL;TYPE=HOME"):content[2] = line[14:].strip()
        elif line.startswith("TEL;TYPE=WORK"):content[3] = line[14:].strip()
        elif line.startswith('TEL'):
            if tel > 15:
                continue
            pos = line.find(':')
            content[tel] = line[pos + 1:].strip()
            tel = tel + 1
        elif line.startswith('END'):
            str = ','.join(content) + '\n'
            wf.write(str)
            content = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                       '', '']
            tel = 4

从各个地方翻了一些代码过来,拼成以上这个,算是0.2版本,记得python里面还有一个显示版本号的version功能,懒得翻书,就不写了。
整理了一下,以前的同事、同行修改为“行业+名字”,可惜以前一些实习生,不知道怎么改好,最后改成这个行业

猜你喜欢

转载自blog.csdn.net/weixin_43221117/article/details/82749182
今日推荐