手机通讯录里面的信息太多,不想让手机里的app读,就导出了一份,用python解析成表格存起来。
经过我的尝试,vobject
这个python库还靠谱一点,经过修饰的vcf文件可以成功读取。
把所有的图片信息干掉了,其实就是头像,没啥用。
另外华为的通讯录导出的信息有些文字数据会折行,无法读取,手动改成一行就可以了。
pip install vobject
import vobject
def getList(val):
return [x.value for x in val]
def getValue(val):
return '"' + '","'.join(getList(val)).replace("vnd.android.cursor.item/",'') + '"'
vcardfile='/home/heihei/pycode/00001.vcf'
with open(vcardfile) as source_file:
vcardlist = vobject.readComponents(source_file)
with open(vcardfile + '.csv','w') as write_file:
for vcard in vcardlist:
print(vcard.contents['fn'][0].value)
if 'tel' in vcard.contents:
print(getValue(vcard.contents['tel']))
write_file.write(vcard.contents['fn'][0].value + ',' + getValue(vcard.contents['tel']) + "\n")
if 'x-android-custom' in vcard.contents:
print(getValue(vcard.contents['x-android-custom']))
write_file.write(vcard.contents['fn'][0].value + ',' + getValue(vcard.contents['x-android-custom']) + "\n")