pyhton与json,Xml
对简单数据类型的encoding 和 decoding:
使用简单的json.dumps方法对简单数据类型进行编码,例如:
1
2
3
4
5
6
|
import json obj = [[ 1 , 2 , 3 ], 123 , 123.123 , 'abc' ,{ 'key1' :( 1 , 2 , 3 ), 'key2' :( 4 , 5 , 6 )}] encodedjson = json.dumps(obj) print repr (obj) print encodedjson
|
from
xml.etree
import
ElementTree
def
print_node(node):
'''打印结点基本信息'''
print
"=============================================="
print
"node.attrib:%s"
%
node.attrib
if
node.attrib.has_key(
"age"
) >
0
:
print
"node.attrib['age']:%s"
%
node.attrib[
'age'
]
print
"node.tag:%s"
%
node.tag
print
"node.text:%s"
%
node.text
def
read_xml(text):
'''读xml文件'''
# 加载XML文件(2种方法,一是加载指定字符串,二是加载指定文件)
# root = ElementTree.parse(r"D:/test.xml")
root
=
ElementTree.fromstring(text)
# 获取element的方法
# 1 通过getiterator
lst_node
=
root.getiterator(
"person"
)
for
node
in
lst_node:
print_node(node)
# 2通过 getchildren
lst_node_child
=
lst_node[
0
].getchildren()[
0
]
print_node(lst_node_child)
# 3 .find方法
node_find
=
root.find(
'person'
)
print_node(node_find)
#4. findall方法
node_findall
=
root.findall(
"person/name"
)[
1
]
print_node(node_findall)
if
__name__
=
=
'__main__'
:
read_xml(
open
(
"test.xml"
).read())
from xml.etree import ElementTree
def print_node(node):
print "=============================================="
print "node.attrib:%s" % node.attrib
if node.attrib.has_key("age") > 0 :
print "node.attrib['age']:%s" % node.attrib['age']
print "node.tag:%s" % node.tag
print "node.text:%s" % node.text
def read_xml(text):
# root = ElementTree.parse(r"<xml><name>wc</name></xml>")
root = ElementTree.fromstring(text)
lst_node = root.getiterator("person")
for node in lst_node:
print_node(node)
lst_node_child = lst_node[0].getchildren()[0]
print_node(lst_node_child)
node_find = root.find('person')
print_node(node_find)
node_findall = root.findall("person/name")[1]
print_node(node_findall)
if __name__ == '__main__':
#read_xml(open("test.xml").read())
read_xml("<person><name>wc</name></person>")
pyhton与json,Xml的更多相关文章
- Python导出Excel为Lua/Json/Xml实例教程(三):终极需求
相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...
- Python导出Excel为Lua/Json/Xml实例教程(一):初识Python
Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...
- JSON&XML总结
JSON&XML: JSON----- //英译 Serialization:序列化 perform:执行 segue:继续 IOS5后 NSJSONSerialization解析 解析JSO ...
- JSON/XML序列化与反序列化(非构造自定义类)
隔了很长时间再重看自己的代码,觉得好陌生..以后要养成多注释的好习惯..直接贴代码..对不起( ▼-▼ ) 保存保存:进行序列化后存入应用设置里 ApplicationDataContainer _a ...
- php返回json,xml,JSONP等格式的数据
php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...
- Atitit.json xml 序列化循环引用解决方案json
Atitit.json xml 序列化循环引用解决方案json 1. 循环引用1 2. 序列化循环引用解决方法1 2.1. 自定义序列化器1 2.2. 排除策略1 2.3. 设置序列化层次,一般3级别 ...
- 【转】[WCF REST] 帮助页面与自动消息格式(JSON/XML)选择
可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehav ...
- 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack
上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...
随机推荐
- hdu 3046 Pleasant sheep and big big wolf 最小割
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...
- UIFontFamily
Family: Hiragino Kaku Gothic ProN W3 Font: HiraKakuProN-W3 Family: Courier Font: Courier ...
- PHP 路径或URL操作
echo 'documentroot:'.$_SERVER['DOCUMENT_ROOT'].'<br>'; //根目录,在apache的配置文件里定义:httpd.conf 比如:Doc ...
- [转]layoutSubviews总结
原文链接找不到了,转的时候别人也是转载的,但并未留下原创链接,就当是笔记了. ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)s ...
- Extjs文本输入域
var form = Ext.create('Ext.form.Panel', { renderTo: Ext.getBody(), frame: tr ...
- MySQL各个版本区别
MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...
- Swift 2.0 到底「新」在哪?
[编者按]2015年6月,一年一度的苹果 WWDC 大会如期而至,在大会上苹果发布了 Swift 2.0,引入了很多新的特性,以帮助开发者更快.更简单地构建应用.本篇文章作者是 Maxime defa ...
- Selenium获取input输入框中值的三种方法
第一种用jQuery的val方法: js = "return $('input').val();" driver.execute_script(js) 第二种用jQuery的att ...
- Acdream1217 Cracking' RSA(高斯消元)
题意:给你m个数(m<=100),每个数的素因子仅来自于前t(t<=100)个素数,问这m个数的非空子集里,满足子集里的数的积为完全平方数的有多少个. 一开始就想进去里典型的dp世界观里, ...
- iOS自定义发送消息输入框
简单的封装了一个,免得麻烦直接初始化就可以用了 ,有其他需求该里面参数就行了 WJEasyInputTextView.h , CGRectGetHeight([UIScreen mainScreen] ...