json 模块 & pickle模块 & shelve模块 & xml模块

# json 模块(将数据转换为通用的json数据)
import json

# 将对象转换为json数据
data = {"name": "alex"}
with open('1.txt', 'w') as f:
    f.write(json.dumps(data))

# 将文件中的json数据取出来
with open('1.txt', 'r') as f_read:
    data = json.loads(f_read.read())
    print(data)

# json.dump
# json.dump(data, f) 就相当于 f.write(json.dumps(data))

# json.load
# data = json.load(f_read) 就相当于 data = json.loads(f_read.read())

 
 
#pickle 模块(将数据转换为python中传递的数据,转换为byte)
import pickle

# 将对象转换为pickle数据
data = {"name": "alex"}
with open('1.txt', 'wb') as f:       #此时必须为b模式
    f.write(pickle.dumps(data))

# 将文件中的pickle数据取出来
with open('1.txt', 'rb') as f_read:
    data = pickle.loads(f_read.read())
    print(data)

# pickle其它方法均与json一样,在此不再多说
# shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象
# 可读可写;key必须为字符串,而值可以是python所支持的数据类型

import shelve

f = shelve.open(r'hello')  #这里的hello相当于文件名,会产生bak,dat,dir三种后缀的文件

# 向文件中写入数据
f['students1'] = {"name": "张三", 'age': 18}
f['students2'] = {"name": "李四", 'age': 17}
f['students3'] = {"name": "王五", 'age': 25}

# 取出存入的数据
print(f.get('students1')['name'])

f.close()   # 关闭shelve对象

Xml文件格式如下

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

# 遍历xml文档 tag:标签;attrib:属性;text:内容
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag, i.attrib, i.text)

# 只遍历year 节点
for node in root.iter('year'):
    print(node.tag, node.text)


# --------------------------------------------------------------

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()

# 修改
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("updated", "yes")

tree.write("xmltest.xml")   # 修改或删除后都需要将内存中的内容重新写入文件

# 删除node
for country in root.findall('country'):      # findall可找到多个,find找一个
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)

tree.write('output.xml')

# --------------------------------------------------------------


# 创建xml文件
import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
sex = ET.SubElement(name, "sex")
sex.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式

猜你喜欢

转载自www.cnblogs.com/dangrui0725/p/9440502.html