10 数据格式

1. 结构化文件存储

  • xml, json,yaml
  • 为了解决不同设备之间信息交换

2. XML

2.1 XML简介
XML(eXtensibleMarkupLanguage), 可扩展标记语言
- 标记语言: 语言中使用尖括号括起来的文本字符串标记
- 可扩展:用户可以自己定义需要的标记
- 是w3c组织制定的一个标准

XML描述的是数据本身,即数据的结构和语义
HTML侧重于如何显示web页面中的数据
<Teacher> 
    自定义标记Teacher
    在两个标记之间任何内容都应该跟Teacher相关
</Teacher>
2.2 XML文档的构成
处理指令(可以认为一个文件内只有一个处理指令)
    以xml关键字开头,最多只有一行,且必须在第一行
    内容是与xml本身处理起相关的一些声明或者指令
    一般用于声明XML的版本和采用的编码
    <?xml version="1.0" encoding="UTF-8"?>

根元素(一个文件内只有一个根元素)
    在整个xml文件中,可以把他看作一个树形结构
    根元素有且只能由一个
子元素
属性
内容
    表明标签所存储的信息
注释
    起说明作用的信息
    注释不能嵌套在标签里
    只有在注释的开始和结尾使用双短横线
    三短横线只能出现在注释的开头而不能用在结尾
<name> <!-- wangdapeng -->   </name> #可以
<name <!-- wangdapeng -->>   </name> #不可以,注释在标签内

<!--my-name-by-wang--> #可以,注释内容可以有一个短横线
<!--my--name--by--wang-->#不可以,双短横线只能出现在开头或结尾

<!---my-name--> #可以, 三短横线只能出现在开头
<!---my-name---> #不可以, 三短横线只能出现在开头        
2.3 保留字符的处理
XML中使用的符号可能跟实际符号相冲突,典型的就是左右尖括号
使用实体引用(EntityReference)来表示保留字符
    <score> score>80 </score> #有错误,xml中不能出现>
    <score> score&gt;80</score> #使用实体引用
把含有保留字符的部分放在CDATA块内部,CDATA块把内部信息视为不需要转义
<![CDATA[
    select name,age from Student where score>80
]]>
常用的需要转移的保留字符和对应实体引用
- &:&amp;
- <:&lt;
- >:&gt;
- ':&apos;
- ":&quot;
- 一共五个, 每个实体引用都以&开头并且以分号结尾
2.4 XML标签的命名规则
Pascal命名法
- 用单词表示,第一个字母大写
- 大小写严格区分
- 配对的标签必须一直
2.5 XML命名空间
- 为了防止命名冲突
<Student>
  <Name>LiuYing</Name>
  <Age>23</Age>
</Student>
<Room>
  <Name>2014</Name>
  <Location>1-23-1</Location>
</Room>
为了避免归并冲突,需要给可能冲突元素添加命名空间
- xmlns: xml name space 的缩写
<Schooler xmlns:student="http://my_student" xmlns:room="http://my_room">
    <student:Name>LiuYing</student:Name>
    <Age>23</Age>
    <romm:Name>2014</room:Name>
    <Location>1-23-1</Location>
</Schooler>

2.6 XML文件读取
XML读取分两个主要技术,SAX, DOM

SAX(Simple API for XML):
- 基于事件驱动的API
- 利用SAX解析文档设计到解析器和事件处理两部分
- 特点:
    - 快
    - 流式读取

DOM
- 是W3C规定的XML编程接口
- 一个XML文件再缓存中以树形结构保存,读取
- 用途
    - 定位浏览XML任何一个节点信息
    - 添加删除相应内容

minidom
    - minidom.parse(filename):加载读取的xml文件, filename也可以是xml代码
    - doc.documentElement:获取xml文档对象,一个xml文件只有一个对于的文档对象
    - node.getAttribute(attr_name):获取xml节点的属性值
    - node.getElementByTagName(tage_name):得到一个节点对象集合
    - node.childNodes:得到所有孩子节点
    - node.childNodes[index].nodeValue:获取单个节点值
    - node.firstNode:得到第一个节点,等价于node.childNodes[0]
    - node.attributes[tage_name]
<?xml version="1.0" encoding="utf-8" ?>
<School>
 <Teacher desc="PythonTeacher" score="good">
 <Name>LiuDana</Name>
 <Age_1 Detail="Age for year 2010">18</Age_1>
 <Mobile>13260446055</Mobile>
 </Teacher> 
 <Student> <Name Other="他是班长">ZhangSan</Name>
 <Age Detail="The yongest boy in class">14</Age>
 </Student>
 <Student> <Name>LiSi</Name>
 <Age>19</Age>
 <Mobile>15578875040</Mobile>
 </Student> 
 <!-- 这是北京图灵学院的一个例子而已 -->

</School>
import xml.dom.minidom
# 负责解析xml文件
from xml.dom.minidom import parse

# 使用minidom打开xml文件
DOMTree = xml.dom.minidom.parse("student.xml")
#得到文档对象
doc = DOMTree.documentElement
print(doc)
# 显示子元素
for ele in doc.childNodes:
    if ele.nodeName == "Teacher":
        print("-------Node:{0}-----".format(ele.nodeName))
        childs = ele.childNodes
        for child in childs:
            if child.nodeName == "Name":
                # data是文本节点的一个属性,表示他的值
                print("Name: {0}".format(child.childNodes[0].data))
            if child.nodeName == "Mobile":
                # data是文本节点的一个属性,表示他的值
                print("Mobile: {0}".format(child.childNodes[0].data))
            if child.nodeName == "Age":
                # data是文本节点的一个属性,表示他的值
                print("Age: {0}".format(child.childNodes[1].data))
                if child.hasAttribute("detail"):
                    print("Age-detail: {0}".format(child.getAttribute("detail")))
etree 
    - 以树形结构来表示xml
    - root.getiterator:得到相应的可迭代的node集合
    - root.iter
    - find(node_name):查找指定node_name的节点,返回一个node
    - root.findall(node_name):返回多个node_name的节点
    - node.tag: node对应的tagename
    - node.text:node的文本值
    - node.attrib: 是node的属性的字典类型的内容
import xml.etree.ElementTree

root = xml.etree.ElementTree.parse("student.xml")
print("利用getiterator访问:")
nodes = root.getiterator()
for node in nodes:
    print("{0}--{1}".format(node.tag, node.text))

print("利用find和findall方法:")
ele_teacher = root.find("Teacher")
print(type(ele_teacher))
print("{0}--{1}".format(ele_teacher.tag, ele_teacher.text))

ele_stus = root.findall("Student")
print(type(ele_stus))
for ele in ele_stus:
    print("{0}--{1}".format(ele.tag, ele.text))
    for sub in ele.getiterator():
        if sub.tag =="Name":
            if "Other" in sub.attrib.keys():
                print(sub.attrib['Other'])


>
利用getiterator访问:
School--
   
Teacher--
       
Name--LiuDana
Age_1--18
Mobile--13260446055
Student--
        
Name--ZhangSan
Age--14
Student--
        
Name--LiSi
Age--19
Mobile--15578875040
利用find和findall方法:
<class 'xml.etree.ElementTree.Element'>
Teacher--
       
<class 'list'>
Student--
        
他是班长
Student--
2.7 xml文件写入
更改
    - ele.set:修改属性
    - ele.append: 添加子元素
    - ele.remove:删除元素
import xml.etree.ElementTree as et
tree = et.parse(r'to_edit.xml')

root = tree.getroot()

for e in root.iter('Name'):
    print(e.text)

for stu in root.iter('Student'):
    name = stu.find('Name')

    if name != None:
        name.set( 'test', name.text * 2)

stu = root.find('Student')

#生成一个新的 元素
e = et.Element('ADDer')
e.attrib = {'a': 'b'}
e.text = '我加的'

stu.append(e)

# 一定要把修改后的内容写回文件,否则修改无效
tree.write('to_edit.xml')
生成创建
- SubElement
import xml.etree.ElementTree as et

stu = et.Element("Student1")

name = et.SubElement(stu, 'Name')
name.attrib = {'lang','en'}
name.text = 'maozedong'

age = et.SubElement(stu, 'Age')
age.text = 18

et.dump(stu)
- minidom 
import xml.dom.minidom

#在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
#创建一个根节点Managers对象
root = doc.createElement('Managers')
#设置根节点的属性
root.setAttribute('company', 'xx科技')
root.setAttribute('address', '科技软件园')
#将根节点添加到文档对象中
doc.appendChild(root)

managerList = [{'name' : 'joy', 'age' : 27, 'sex' : '女'},
  {'name' : 'tom', 'age' : 30, 'sex' : '男'},
  {'name' : 'ruby', 'age' : 29, 'sex' : '女'}
]

for i in managerList :
    nodeManager = doc.createElement('Manager')
    nodeName = doc.createElement('name')
    #给叶子节点name设置一个文本节点,用于显示文本内容
    nodeName.appendChild(doc.createTextNode(str(i['name'])))

    nodeAge = doc.createElement("age")
    nodeAge.appendChild(doc.createTextNode(str(i["age"])))

    nodeSex = doc.createElement("sex")
    nodeSex.appendChild(doc.createTextNode(str(i["sex"])))

    #将各叶子节点添加到父节点Manager中,
    #最后将Manager添加到根节点Managers中
    nodeManager.appendChild(nodeName)
    nodeManager.appendChild(nodeAge)
    nodeManager.appendChild(nodeSex)
    root.appendChild(nodeManager)
    
#开始写xml文档
fp = open('Manager.xml', 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
- etree创建
import xml.etree.ElementTree as et

#在内存中创建一个空的文档

etree = et.ElementTree()

e = et.Element('Student')

etree._setroot(e)

e_name = et.SubElement(e, 'Name')
e_name.text = "hahahah"

etree.write('v06.xml')
2.8 参考资料
https://docs.python.org/3/library/xml.etree.elementtree.html
http://www.runoob.com/python/python-xml.html
https://blog.csdn.net/seetheworld518/article/details/49535285

3. JSON

3.1 JSON简介
JSON(JavaScriptObjectNotation)
- 轻量级的数据交换格式,基于ECMAScript
- json格式是一个键值对形式的数据集
    - key: 字符串
      - value:字符串,数字,列表,json
      - json使用大括号包裹
      - 键值对直接用都好隔开
 student={ "name": "wangdapeng", "age": 18, "mobile":"13260446055" } 
3.2 JSON文件读取
json和python格式的对应
  - 字符串:字符串
  - 数字:数字
  - 队列:list
  - 对象:dict
  - 布尔值:布尔值
python for json
  - json包
  - json和python对象的转换
  - json.dumps():对数据编码,把python格式表示成json格式
  - json.loads(): 对数据解码,把json格式转换成python格式
python读取json文件
  - json.dump(): 把内容写入文件
  - json.load(): 把json文件内容读入python
import json

# 此时student是一个dict格式内容,不是json
student={
    "name": "luidana",
    "age": 18,
    "mobile":"15578875040"
}

print(type(student))

stu_json = json.dumps(student)
print(type(stu_json))
print("JSON对象:{0}".format(stu_json))

stu_dict = json.loads(stu_json)
print(type(stu_dict))
print(stu_dict)
import json

data = {"name":"hahah", "age":12}

with open("t.json", 'w') as f:
    json.dump(data, f)

with open("t.json", 'r') as f:
    d = json.load( f)
    print(d)
3.3 json在线工具
https://www.sojson.com/
http://www.w3school.com.cn/json/
http://www.runoob.com/json/json-tutorial.html

4. YAML

4.1 Yaml简介
简洁的非标机语言
以数据为中心,使用空白、缩进、分行组织数据

特点:
    大小写敏感使用
    使用**空格键**缩进表示层级关系(不可使用tab缩进)
    使用引号表注释
    字符串可以不使用引号
4.2 yaml数据结构
1.字典
使用(: )表示键值对,同一缩进的所有键值对对应同一个map
    paltformName: Android
    paltformVersion: 5.1     ## 注意冒号后的空格
    或:
    {paltformName:Android,paltformVersion:5.1}
2.列表
使用(- )表示,注意-后的空格
    - hello
    - world
3.scalsr,纯量
4.3 yaml文件读取
python读取yaml文件库:PyYmal,ruamel.yaml
# demo.yaml

name: hello
sex: female
age: 18
area: shanghai
hobby:
  - 睡觉
  - 吃饭
  -
     game1: 王者荣耀
     game2: 吃鸡
# demo.py

import yaml
import os

fs = open(os.getcwd() + "\demo.yaml", encoding="utf-8")
datas = yaml.load(fs, Loader=yaml.FullLoader)

print(datas)

>{'name': 'hello', 'sex': 'female', 'age': 18, 'area': 'shanghai', 'hobby': ['睡觉', '吃饭', {'game1': '王者荣耀', 'game2': '吃鸡'}]}

猜你喜欢

转载自blog.csdn.net/qq_25672165/article/details/89068740
今日推荐