• Python --解析xml


    目录

    1, sax解析xml


    1, sax解析xml

    dom会把整个xml文件读入到内存占用内存大sax是流模式, 边读边解析,占用内存小

    1. #! /usr/bin/env/python3
    2. # -*- coding: utf-8 -*-
    3. '''解析xml中的天气字段'''
    4. from xml.parsers.expat import ParserCreate
    5. xml = '''
    6. 1
    7. 1
    8. OK
    9. 10000
    10. 北京市
    11. 110000
    12. 北京
    13. 2021-05-14 18:33:48
    14. 2021-05-14
    15. 5
    16. 多云
    17. 小雨
    18. 26
    19. 17
    20. 东南
    21. 东南
    22. ≤3
    23. ≤3
    24. 2021-05-15
    25. 6
    26. 小雨
    27. 小雨
    28. 21
    29. 14
    30. 4
    31. 4
    32. 2021-05-16
    33. 7
    34. 多云
    35. 25
    36. 13
    37. 西
    38. 西
    39. ≤3
    40. ≤3
    41. 2021-05-17
    42. 1
    43. 30
    44. 15
    45. 西南
    46. 西南
    47. ≤3
    48. ≤3
    49. '''
    50. class MySaxHandler(object):
    51. def __init__(self, city='Beijing'):
    52. # 城市字段
    53. self.city = city
    54. # 一天天气
    55. self.one_day_weather = {}
    56. # 所有天天气
    57. self.all_day_weather = []
    58. # 指定城市的所有天气
    59. self.weather = {'city': self.city, 'forecasts': self.all_day_weather}
    60. # 天气字段
    61. self.weather_attrs_list = ['date', 'week', 'dayweather', 'nightweather',
    62. 'daytemp', 'nighttemp', 'daywind', 'nightwind']
    63. def start_element_handler(self, name, attrs):
    64. '''
    65. 开始标签处理方法
    66. :param name: 标签名称
    67. :param attrs: 标签属性
    68. :return:
    69. '''
    70. self.start_element_name = name
    71. self.start_element_attrs = attrs
    72. def end_element_handler(self, name):
    73. '''
    74. 结束标签处理方法
    75. :param name: 标签名称
    76. :return:
    77. '''
    78. self.end_element_name = name
    79. if self.end_element_name in self.weather_attrs_list:
    80. self.one_day_weather[self.end_element_name] = self.text
    81. if len(self.one_day_weather) == len(self.weather_attrs_list):
    82. self.all_day_weather.append(self.one_day_weather)
    83. self.one_day_weather = {}
    84. def chardata_handler(self, text):
    85. '''
    86. 标签信息处理方法
    87. :param text: 标签信息
    88. :return:
    89. '''
    90. self.text = text
    91. def parser_xml(xml):
    92. my_handler = MySaxHandler()
    93. parser = ParserCreate()
    94. parser.StartElementHandler = my_handler.start_element_handler
    95. parser.EndElementHandler = my_handler.end_element_handler
    96. parser.CharacterDataHandler = my_handler.chardata_handler
    97. # 解析xml
    98. parser.Parse(xml)
    99. return my_handler.weather
    100. if __name__ == "__main__":
    101. result = parser_xml(xml)
    102. print(result)
    103. ----------------------------------------------------------------------------------
    104. # 输出
    105. "C:\Program Files\Python311\python.exe" D:\python_core_programming_learn\chapter_7_dict_set\notes.py
    106. {'city': 'Beijing', 'forecasts': [{'date': '2021-05-14', 'week': '5', 'dayweather': '多云', 'nightweather': '小雨', 'daytemp': '26', 'nighttemp': '17', 'daywind': '东南', 'nightwind': '东南'}, {'date': '2021-05-15', 'week': '6', 'dayweather': '小雨', 'nightweather': '小雨', 'daytemp': '21', 'nighttemp': '14', 'daywind': '北', 'nightwind': '北'}, {'date': '2021-05-16', 'week': '7', 'dayweather': '多云', 'nightweather': '晴', 'daytemp': '25', 'nighttemp': '13', 'daywind': '西', 'nightwind': '西'}, {'date': '2021-05-17', 'week': '1', 'dayweather': '晴', 'nightweather': '晴', 'daytemp': '30', 'nighttemp': '15', 'daywind': '西南', 'nightwind': '西南'}]}

  • 相关阅读:
    鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    自学java怎么入门?
    Spring源码里开天辟地的五个Bean,再介绍一个学习方法
    STM32H7 USART 时钟初始化
    37-Java方法的内存原理
    数据库设计 Relational Language
    26 JavaScript模块
    【生日快乐】SpringBoot SpringBoot 提高篇(第二篇) 第5章 SpringBoot 日志 5.4 日志格式
    使用OpenVINOTM预处理API进一步提升YOLOv5推理性能
    移远通信推出高性能九合一5G组合天线
  • 原文地址:https://blog.csdn.net/qq_25500415/article/details/133361680