Dive into Python第九章xml学习笔记
来源:互联网 发布:保定网络推广 编辑:程序博客网 时间:2024/05/22 09:47
这两天搞了一伙scrapy,感觉对保存下来的xml处理不是很好,就又看了看书,总体感觉第九章对xml介绍还是太简单。
1. python对xml的操作主要有两个方法,一个叫SAX(Simple API for XML), 它一次读取少量xml数据调用对应函数进行操作;另一个叫DOM(Document Object Model), 一次读取全部数据,把xml转成python的格式(转成树结构)。(其实在cookbook中还有一个lib叫expat)。
2. 在文件目录中添加__init__.py directory就成为了包,所以:
from xml.dom import minidom #如果这么导入模块,不仅会导入xml.dom中minidom.py,还会导入dom目录下__init__.py,再所以:
#如果你打开__init__.py,会看到:
from domreg import getDOMImplementation,registerDOMImplementation #它也会自动导入,再再所以:
#我在看scrapy的时候,发现scrapy根本没有Field这个class,其实是:
import scrapy #scrapy中没有Field这个class,其实它的__init__.py有一条: from scrapy.item import Item, Fieldclass Product(scrapy.Item): #所以:name = scrapy.Field() #scrapy.Field()=scrapy.Item.Filed()price = scrapy.Field()stock = scrapy.Field()
3. 最简单的解析xml方法:
xmldoc = minidom.parse(file-like obj) #返回document类型的树形结构,它有且只有一个子点,就是它的root node
4. unicode是两个8B组成的编码,不同的编码形式可以将其显示不同语言,系统默认是ASCII。python 2.7 print能很好的显示unicode,默认是ASCII,也能显示Latin-1
>>> s = u'La Pe\xf1a'>>> print sLa Peña
#不然就需要:
>>> print s.encode('latin−1')
5. 中文简体是GB2312.
6. 如果.py中存储非ASCII码,那么就需在文头加上 # −*− coding: UTF−8 −*−。xml都是unicode存储数据。
7. Element.getElmentByTagName('ref') 可以找到所有‘ref’tag的元素,返回list形式。
8. 要得到Element的属性, 用Element.attributes.keys()得到id, Element.attributes.values()不能得到value,需要Element.attributes['id'].value !!!
0 0
- Dive into Python第九章xml学习笔记
- Dive into python第七章正则表达式学习笔记
- Dive Into Python学习日志
- Dive into python 第5和6章面向对象学习笔记
- Dive into python第四章自省的威力笔记
- [Dive into Python:第三章]内置数据类型
- Dive Into Python
- Dive into Python 点滴
- dive into python (2)
- dive into python (3)
- dive into python
- Python 研究(Dive Into Python)笔记1--第2章 第一个 Python 程序
- Python 研究(Dive Into Python)笔记2--第3章 内置数据类型
- 深入Python3 (Dive Into Python3)笔记12--XML
- dive into Python (1)
- 《dive into python3》 笔记摘录
- Dive in Python学习笔记八
- Dive in Python学习笔记九
- 关于在学习知识和运用知识中的一些个人的见解(后续会持续更新)
- UVA11838- Come and Go
- 【C++杂谈0】记录一些我容易犯错的C++细节
- 内存优化之RAM
- Struts2,Hibernate和Spring之间的框架整合
- Dive into Python第九章xml学习笔记
- [Attila GPU] Attila OGL2/D3D9 GPU C Model Simulator
- Begining_2014-10-16
- mac上安装win7虚拟机及其理论依据
- Objective-C的Initialize初始化方法研究
- 网络常见的安全威胁和攻击手段
- 第八周上机项目2
- AXIS:使用web service进行文件的上传
- struts+spring+hibernate(SSH)框架中spring到底起什么作用啊?它与struts+hibernate(SH)框架相比又有哪些优缺