1. 程式人生 > >記錄一次手機聯系人整理(XML文件格式處理)

記錄一次手機聯系人整理(XML文件格式處理)

[] tro 等號 enum 刪除 global root 資源文件 nbsp

場景:1、IOS手機和Android手機聯系人同步時有部分重復聯系人。

   2、很早以前的HTC手機導出的聯系人中備註信息有大量亂碼,且很多聯系人生日被設置為1970-01-01,導致生日提醒軟件產生騷擾問題。

適用設備平臺:IOS,Android

聯系人備份軟件:騰訊應用寶

關鍵字:Python,xml

整體步驟:

  1、下載騰訊應用寶(非打廣告,該支持excel,xml,csv等多種格式的聯系人備份文件導入導出)

  2、手機連接PC,打開應用寶,導出聯系人為xml文件。

  3、代碼實現,生成新的聯系人xml文件Contactrsp.xml:

 1 #!/usr/bin/env python  
2 # encoding: utf-8 3 """ 4 @version: v1.0 5 @author: Elijahxb 6 @time: 2017/12/3 12:13 7 """ 8 import xml.etree.ElementTree as ET 9 from lib.Global.GlobalPath import *#可自行定義資源文件路徑變量resource_path 10 import os 11 12 xmlPath = os.path.join(resource_path,"XML","FullContact.xml") 13 tree = ET.parse(xmlPath)
14 root = tree.getroot() 15 con_node_list = root.findall("Contact") 16 print len(con_node_list) 17 index = 0 18 con_dic = {} 19 for con_node in con_node_list: 20 PhoneList_node = con_node.find("PhoneList") 21 if PhoneList_node is not None: 22 con_name = con_node.find("Name").text 23 PhoneNumber_list = PhoneList_node.findall("
Phone") 24 if PhoneNumber_list: 25 con_dic[con_name] = [] 26 for PhoneNumber in PhoneNumber_list: 27 telephone = PhoneNumber.text 28 if telephone[:4] == ("+86-"): 29 telephone = telephone[4:] 30 con_dic[con_name].append(telephone) 31 elif telephone[:3] == "400" or telephone[:4] == "0400" or telephone[:3] == "106": 32 pass 33 elif telephone == "18000000000": 34 pass 35 elif u"專線" in con_name: 36 pass 37 else: 38 con_dic[con_name].append(telephone) 39 if con_dic[con_name] == []: 40 con_dic.pop(con_name) 41 index += 1 42 print len(con_dic) 43 44 45 root = ET.Element("Contacts") 46 for k in con_dic: 47 print k,con_dic[k] 48 Contact_node = ET.SubElement(root,"Contact") 49 Name_node = ET.SubElement(Contact_node,"Name") 50 Name_node.text = k 51 PhoneList_node = ET.SubElement(Contact_node,"PhoneList") 52 for telephone in con_dic[k]: 53 phone_node = ET.SubElement(PhoneList_node,"Phone",{"Type":"2"}) 54 phone_node.text = telephone 55 56 tree = ET.ElementTree(root) 57 f_path = os.path.join(resource_path,"XML","Contactrsp.xml") 58 tree.write(f_path,encoding="utf-8")

  4、刪除手機聯系人,導入Contactrsp.xml文件,結束。

  5、實現效果:a.同名聯系人的號碼會被合並到同名聯系人下。b.自定義過濾器,將攔截軟件生成的400,106等號碼刪除,名稱為**專線**的號碼刪除。

記錄一次手機聯系人整理(XML文件格式處理)