想写一个用 Python 来抽取 xml 文件中的一些数据并转码
xml 如下
<?xml version="1.0" encoding="utf-8"?>
<config>
<HTTP>
<SITE NAME="HUNLIAN" DESCRIPTION="%E5%A9%9A%E6%81%8B" >
</SITE>
<SITE NAME="jiaoyou" DESCRIPTION="%E5%A9%9AB" >
</SITE>
</HTTP>
</config>
Python 如下
```
#!/usr/bin/python
#--coding:utf-8--
from xml.dom.minidom import parse
import xml.dom.minidom
import urllib
DOMTree = xml.dom.minidom.parse("xff.xml")
Data = DOMTree.documentElement
Sites = Data.getElementsByTagName("SITE")
for site in Sites:
L = "name: %s " %site.getAttribute("NAME")
M = "desc: %s" %site.getAttribute("DESCRIPTION")
url = urllib.unquote(M[0])
print L,M
f = open("info.txt", "a")
f.write(L+'\n'+M+'\n'+url+'\n')
f.close
结果如下:
name: HUNLIAN
desc: %E5%A9%9A%E6%81%8B
M(0)
name: jiaoyou
desc: %E5%A9%9AB
M(0)
实际想要的是 M(0)应该是 desc 的 URL 解码
1
zhyu 2016-01-12 22:36:23 +08:00
```python
>>> import urllib >>> print urllib.unquote('%E5%A9%9A%E6%81%8B').decode('utf8') 婚恋 ``` |
2
mnsw OP |