博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的xml模块
阅读量:5260 次
发布时间:2019-06-14

本文共 3348 字,大约阅读时间需要 11 分钟。

cat an_xml.py 

#!/usr/bin/python3import xml.saxclass MovieHandler( xml.sax.ContentHandler ):   def __init__(self):      self.CurrentData = ""      self.type = ""      self.format = ""      self.year = ""      self.rating = ""      self.stars = ""      self.description = ""   # 元素开始调用   def startElement(self, tag, attributes):      self.CurrentData = tag      if tag == "movie":         print ("*****Movie*****")         title = attributes["title"]         print ("Title:", title)   # 元素结束调用   def endElement(self, tag):      if self.CurrentData == "type":         print ("Type:", self.type)      elif self.CurrentData == "format":         print ("Format:", self.format)      elif self.CurrentData == "year":         print ("Year:", self.year)      elif self.CurrentData == "rating":         print ("Rating:", self.rating)      elif self.CurrentData == "stars":         print ("Stars:", self.stars)      elif self.CurrentData == "description":         print ("Description:", self.description)      self.CurrentData = ""   # 读取字符时调用   def characters(self, content):      if self.CurrentData == "type":         self.type = content      elif self.CurrentData == "format":         self.format = content      elif self.CurrentData == "year":         self.year = content      elif self.CurrentData == "rating":         self.rating = content      elif self.CurrentData == "stars":         self.stars = content      elif self.CurrentData == "description":         self.description = content  if ( __name__ == "__main__"):      # 创建一个 XMLReader   parser = xml.sax.make_parser()   # turn off namepsaces   parser.setFeature(xml.sax.handler.feature_namespaces, 0)   # 重写 ContextHandler   Handler = MovieHandler()   parser.setContentHandler( Handler )      parser.parse("movies.xml")

cat movies.xml 

War, Thriller
DVD
2003
PG
10
Talk about a US-Japan war
Anime, Science Fiction
DVD
1989
R
8
A schientific fiction
Anime, Action
DVD
4
PG
10
Vash the Stampede!
Comedy
VHS
PG
2
Viewable boredom

运行结果python3 an_xml.py:

*****Movie*****Title: Enemy BehindType: War, ThrillerFormat: DVDYear: 2003Rating: PGStars: 10Description: Talk about a US-Japan war*****Movie*****Title: TransformersType: Anime, Science FictionFormat: DVDYear: 1989Rating: RStars: 8Description: A schientific fiction*****Movie*****Title: TrigunType: Anime, ActionFormat: DVDRating: PGStars: 10Description: Vash the Stampede!*****Movie*****Title: IshtarType: ComedyFormat: VHSRating: PGStars: 2Description: Viewable boredom

中间发生错误是因为文件名用xml命名,导致:

No module named 'xml.sax'; 'xml' is not a package

还有一个文件拷贝的格式问题:

-bash: ./xml.py: /usr/bin/python3^M: bad interpreter: No such file or directory

解决:

dos2unix  an_xml.py

转载于:https://www.cnblogs.com/zw2002/p/8423159.html

你可能感兴趣的文章
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>
趣谈Java变量的可见性问题
查看>>
C# 强制关闭当前程序进程(完全Kill掉不留痕迹)
查看>>
ssm框架之将数据库的数据导入导出为excel文件
查看>>
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>