对简单数据类型的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的更多相关文章

  1. Python导出Excel为Lua/Json/Xml实例教程(三):终极需求

    相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...

  2. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  3. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  4. JSON&XML总结

    JSON&XML: JSON----- //英译 Serialization:序列化 perform:执行 segue:继续 IOS5后 NSJSONSerialization解析 解析JSO ...

  5. JSON/XML序列化与反序列化(非构造自定义类)

    隔了很长时间再重看自己的代码,觉得好陌生..以后要养成多注释的好习惯..直接贴代码..对不起( ▼-▼ ) 保存保存:进行序列化后存入应用设置里 ApplicationDataContainer _a ...

  6. php返回json,xml,JSONP等格式的数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  7. Atitit.json xml 序列化循环引用解决方案json

    Atitit.json xml 序列化循环引用解决方案json 1. 循环引用1 2. 序列化循环引用解决方法1 2.1. 自定义序列化器1 2.2. 排除策略1 2.3. 设置序列化层次,一般3级别 ...

  8. 【转】[WCF REST] 帮助页面与自动消息格式(JSON/XML)选择

    可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehav ...

  9. 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack

    上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...

随机推荐

  1. LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  2. ptypes中string类的空间分配

    问题描述:            在学习ptypes中string类的空间分配时,经常使分配的空间超出实际所需的空间 使用的分配函数是:_alloc函数 注:        在_alloc函数中调用了 ...

  3. bzoj 1270 DP

    w[i,j]代表高度j,第i颗树的时候的最大值 那么w[i,j]:=max(w[i,j+1],w[k,j+heigh])+sum[i,j]: 但是这样枚举是n^3的,我们发现转移的第二个选择w[k,j ...

  4. 【POJ】【1160】Post Office

    DP/四边形不等式 邮局,经典的四边形不等式例题! 关于四边形不等式的学习请看 赵爽论文<动态规划加速原理之四边形不等式> 题目总结&题解:http://blog.csdn.net ...

  5. linux centos yum 安装 rar

    linux yum安装rar时,可能会出现无资源的错误,只需把配置好资源即可,具体操作如下: 1.# vi /etc/yum.repos.d/dag.repo 2.将以下内容写入文件中 [dag] n ...

  6. php __FILE__,__CLASS__等魔术变量,及实例(转)

    今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量 1,__FILE__ 文件的完整路径和文件名.如果用在被包含文件 ...

  7. UML概述(转载)

    UML是一种标准语言,用于指定,可视化,构造和文档的软件系统. UML是OMG在1997年1月提出了创建由对象管理组织(OMG)和UML1.0规范草案. OMG不断努力,使一个真正的行业标准. UML ...

  8. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  9. POJ 1631

    #include <iostream> #define MAXN 500005 using namespace std; int T[MAXN]; int binary_search(in ...

  10. ExtJs布局之Card

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...