XML原始数据

<dataList>
    <contactList>
        <contact>
            <contactName>张三</contactName>
            <contactPhone>135 0001 0001</contactPhone>
        </contact>
        <contact>
            <contactName>李四</contactName>
            <contactPhone>135 0001 0001</contactPhone>
        </contact>
        <contact>
            <contactName>王五</contactName>
            <contactPhone>132 0001 0001</contactPhone>
        </contact>
    </contactList>
    <appInfo>
        <app>
            <appName>短信</appName>
            <appPackage>com.android.providers.telephony</appPackage>
            <appVersion>5.1.1</appVersion>
            <appFileSize>32786</appFileSize>
            <appInstallTime>1487072971000</appInstallTime>
        </app>
        <app>
            <appName>收音机</appName>
            <appPackage>com.miui.fm</appPackage>
            <appVersion>5.1.1</appVersion>
            <appFileSize>327862</appFileSize>
            <appInstallTime>1487072971000</appInstallTime>
        </app>
    </appInfo>
</dataList>

模块简单描述

编写脚本

# -*- coding:utf-8 -*-
# author:mr_fojo

import xml.etree.ElementTree as ET
import time

tree = ET.parse('readdata.xml')    #设置需要读取的文件
root = tree.getroot()   #获取根节点

# 读取短信内容
def CONTACTLIST():
    sum = 0
    #   获取二级节点的内容
    for child in root.findall("contactList"):
        #   获取三级节点的内容
        for contactName in child.findall("contact"):
            #   获取四级节点的内容
            Name = contactName.find("contactName").text
            Tel = contactName.find("contactPhone").text
            sum += 1
            print(sum,Name,Tel.replace(' ','')) # 去除电话中的空格

# 时间戳转换成日期格式(原单位为毫秒级,所以要除以1000)
def custom_time(timestamp):
    time_local = time.localtime(timestamp / 1000)
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    return dt

# 读取应用信息
def APPINFO():
    sum = 0
    #   获取二级节点的内容
    for child in root.findall("appInfo"):
        #   获取三级节点内容
        for contactName in child.findall("app"):
            #   获取四级节点内容
            APPName = contactName.find("appName").text
            APPVersion = contactName.find("appVersion").text
            APPPckeg= contactName.find("appPackage").text
            APPInstallTime = contactName.find("appInstallTime").text
            sum += 1
            print(sum,custom_time(int(APPInstallTime)))

APPINFO()
Last modification:September 17th, 2019 at 05:26 pm
If you think my article is useful to you, please feel free to appreciate