场景:1、IOS手机和Android手机联系人同步时有部分重复联系人。
2、很早以前的HTC手机导出的联系人中备注信息有大量乱码,且很多联系人生日被设置为1970-01-01,导致生日提醒软件产生骚扰问题。
适用设备平台:IOS,Android
联系人备份软件:腾讯应用宝
关键字:Python,xml
整体步骤:
1、下载腾讯应用宝(非打广告,该支持excel,xml,csv等多种格式的联系人备份文件导入导出)
2、手机连接PC,打开应用宝,导出联系人为xml文件。
3、代码实现,生成新的联系人xml文件Contactrsp.xml:
#!/usr/bin/env python
# encoding: utf-8
"""
@version: v1.0
@author: Elijahxb
@time: 2017/12/3 12:13
"""
import xml.etree.ElementTree as ET
from lib.Global.GlobalPath import *#可自行定义资源文件路径变量resource_path
import os xmlPath = os.path.join(resource_path,"XML","FullContact.xml")
tree = ET.parse(xmlPath)
root = tree.getroot()
con_node_list = root.findall("Contact")
print len(con_node_list)
index = 0
con_dic = {}
for con_node in con_node_list:
PhoneList_node = con_node.find("PhoneList")
if PhoneList_node is not None:
con_name = con_node.find("Name").text
PhoneNumber_list = PhoneList_node.findall("Phone")
if PhoneNumber_list:
con_dic[con_name] = []
for PhoneNumber in PhoneNumber_list:
telephone = PhoneNumber.text
if telephone[:4] == ("+86-"):
telephone = telephone[4:]
con_dic[con_name].append(telephone)
elif telephone[:3] == "" or telephone[:4] == "" or telephone[:3] == "":
pass
elif telephone == "18000000000":
pass
elif u"专线" in con_name:
pass
else:
con_dic[con_name].append(telephone)
if con_dic[con_name] == []:
con_dic.pop(con_name)
index += 1
print len(con_dic) root = ET.Element("Contacts")
for k in con_dic:
print k,con_dic[k]
Contact_node = ET.SubElement(root,"Contact")
Name_node = ET.SubElement(Contact_node,"Name")
Name_node.text = k
PhoneList_node = ET.SubElement(Contact_node,"PhoneList")
for telephone in con_dic[k]:
phone_node = ET.SubElement(PhoneList_node,"Phone",{"Type":""})
phone_node.text = telephone tree = ET.ElementTree(root)
f_path = os.path.join(resource_path,"XML","Contactrsp.xml")
tree.write(f_path,encoding="utf-8")
4、删除手机联系人,导入Contactrsp.xml文件,结束。
5、实现效果:a.同名联系人的号码会被合并到同名联系人下。b.自定义过滤器,将拦截软件生成的400,106等号码删除,名称为**专线**的号码删除。